Sub select in LINQ

Hi,
I'm trying to write a sub select in linq to generete the list of
users.

I have an XML like this:

<?xml version="1.0" encoding="utf-8"?>
<UserConfig>
  <Users>
    <User id="1" loginId="arun" firstName="Arunkumar"
lastName="Dharuman" >
      <assignedRoles>
        <id>1</id>
      </assignedRoles>
    </User>
    <User id="2" loginId="arun" firstName="Arunkumar"
lastName="Dharuman" >
      <assignedRoles>
        <id>1</id>
        <id>2</id>
      </assignedRoles>
    </User>
    <User id="3" loginId="arun" firstName="Arunkumar1"
lastName="Dharuman1" >
      <assignedRoles>
        <assignedRoles>
          <id>3</id>
          <id>2</id>
        </assignedRoles>
      </assignedRoles>
    </User>
  </Users>
</UserConfig>

I'm following query works fine when assignedRole is ignored...

var users = from ul in xDoc.Descendants("User")
                         select new
                         {
                               Id = ul.Attribute("id"),
                               LoginName = ul.Attribute("loginId"),
                               FirstName = ul.Attribute("firstName"),
                               LastName = ul.Attribute("lastName"),
                         };

when trying to include assignedRole as

var users = from ul in xDoc.Descendants("User")
                         select new
                         {
                               Id = ul.Attribute("id"),
                               LoginName = ul.Attribute("loginId"),
                               FirstName = ul.Attribute("firstName"),
                               LastName = ul.Attribute("lastName"),
                               Roles = from ur in ul.Element
("assignedRoles")
                                        select new
                                        {
                                               RoleId = ur.Element
("id")
                                        };
                         };

so that the following code should work:

foreach (var user in users)
{
  foreach(var role in Roles)
  {
  }
}

Regards,
ArunDhaJ

0
ArunDhaJ
12/7/2009 1:40:09 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

1 Replies
1403 Views

Similar Articles

[PageSpeed] 2

ArunDhaJ wrote:

>     <User id="2" loginId="arun" firstName="Arunkumar"
> lastName="Dharuman" >
>       <assignedRoles>
>         <id>1</id>
>         <id>2</id>
>       </assignedRoles>

> when trying to include assignedRole as
> 
> var users = from ul in xDoc.Descendants("User")
>                          select new
>                          {
>                                Id = ul.Attribute("id"),

Are you sure you want to store the 'id' XAttribute? I would rather go 
with                             Id = (int)ul.Attribute("id")

>                                LoginName = ul.Attribute("loginId"),
>                                FirstName = ul.Attribute("firstName"),
>                                LastName = ul.Attribute("lastName"),
>                                Roles = from ur in ul.Element
> ("assignedRoles")

I think you want
                                  Roles = from rid in 
ul.Element("assignedRoles").Elements("id")
                                     select (int)rid

or if you want to create an anonymous type then
                                  Roles = from rid in 
ul.Element("assignedRoles").Elements("id")
                                     select new { RoleId = (int)rid }


-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
Martin
12/7/2009 3:14:28 PM
Reply:

Similar Artilces:

Multiple Charts; How to Select Colors?
I have a worksheet that contains several pie charts. I would like the color scheme of the pie slices to be different on each of the charts. How do I change the color format on each pie chart so that the color is individualized? Thanks Bill Falzone Hi, Color each segment manually on each of the charts. To color an individual slice click the pie series and then single click a slice. When one segment is selected double-click it and change it color or float the color palette and click the color of your choice. Repeat for each slice on each chart. If you are using 2007 the idea is th...

I selected row 1 and row 2 to freeze and it is freezing at row 12
I am selecting row 1 and row 2 to freeze and excel keeps freezing at row 12? Select one row only, if you want 2 rows above where it freezes select row 3, that will leave row1 and 2 unaffected when you scroll down -- Regards, Peo Sjoblom "Lori Brooks" <Lori Brooks@discussions.microsoft.com> wrote in message news:C968CA9B-35A3-427E-993C-D36F3B6D37B7@microsoft.com... >I am selecting row 1 and row 2 to freeze and excel keeps freezing at row >12? Hi, To freeze row 1 & 2 select A3 and then Window|Freeze Panes Mike "Lori Broo...

New to Sub Domains
In AD I am planing to create a sub domain. I have have HQ Users in a root domain "domain.local" and then an Orlando office in a sub domain "Orlando.domain.local". The Orlando users currently have accounts in the root domain with Exchange mailboxes boxes. I am plaining a local exchange in Orlando in this new domain but: 2 Questions: Any "gotcha's" to putting an exchange server in "Orlando.domain.local" and allowing both the exchange servers to get inbound external email for the same public domain name? How do I handle the user migration? Cre...

Can I change Headers and footers in selected Reports at the same .
maybe... If you're writing about worksheets in the same workbook, you can group the sheets (click on the first tab and ctrl-click on subsequent tabs). Then File|page setup and change the headers/footers. Remember to ungroup the sheets (just rightclick on any of the grouped sheets and select ungroup sheets) or you may damage your workbook. Almost anything you do to a one sheet (of a group) is done to the rest of the sheets in that group. Tester wrote: -- Dave Peterson Thanks Dave, but no, I need to change headers and footers in over a hundred different folders on a regular bas...

MS Money 2005. Sub-Categories not shown on transaction form
I reformatted my hard-drive and lost my registry setting that allows the sub-category to be shown on the transaction form like older versions of MS Money. On the original install I got on this site and asked how to set the registry setting that allowed the sub-cat to show but I cannot find my old post and did not keep the instructions on how to do it. Anyone know? Thanks, rroot In microsoft.public.money, rroot01 wrote: >I reformatted my hard-drive and lost my registry setting that allows the >sub-category to be shown on the transaction form like older versions of MS >Money....

Combo Box to select a record and populate fields in a form
I have created a form where I've created a combo box that has a drop down list of box numbers where I want to be able to select one box number and have it populate the other fields on the form. On the combo box, I have entered the following for "after update", but it doesn't allow me to select a record and doesn't populate any of the other fields for that record. Sub Combo74_AfterUpdate() ' Find the record that matches the control. Me.Filter = "[BoxNo] = """ & Me.Combo74 & """" Me.FilterOn =...

How do i select data from a validation list without scrolling?
I need to execute data from a large validation list whithout having to scroll for ages to find what i need. Ramchi DV list length cannot be changed from the default 8 items. For work-arounds see Debra Dalgleish's site for a Free download of a workbook showing how to use a Combo-box in conjunction with the DV cell. http://www.contextures.on.ca/xlDataVal08.html#Font Gord Dibben Excel MVP On Sat, 9 Oct 2004 14:49:02 -0700, Ramchi <Ramchi@discussions.microsoft.com> wrote: >I need to execute data from a large validation list whithout having to scroll >for ages to find what...

Automating Selection of Graphs for Printing
I have a workbook containing cost data for up to 20 client plans. Each plan's data generates three different graphs, which are collected on three separate worksheets. Thus, sheet 1= 20 plans worth of data input, sheet 2= 20 graphs (type 1), sheet 3= 20 graphs (type 2), and sheet 4= 20 graphs (type 3). Once the data is entered, any assorted grouping of graphs might need to be printed at one time. It has been a pain to scroll around and search for the graphs needed for printing out. Since there are dozens of the self-contained workbooks I've described, I'd like to auto...

Macro command to move cell selection
I am writing a macro for a spreadsheet where I have values stored in one column. I want to run a loop where each time the loop runs, I select each cell in the column. For example, my values are stored in column A, cells 1 through 7. On loop 1 I want to select cell A1, loop 2 select cell A2, loop 3 select A3 and so on. How can I set this up in a macro to increment cell selection as I move through my loop? I'm really not sure whay you can do with this, but is this what you are wanting? Sub SelectionLoop() Dim i As Long For i = 1 To 7 Cells(i, "A"...

how to set up a query to show only user based selections
I have created a table with data types yes/no. I am trying to create a query that ONLY shows when the user has selected yes. I am trying to create a report that only shows the options selected versus showing all of the options. The users select this from a form that has option buttons. Any help will be greatlt appreciated! Submitted via EggHeadCafe - Software Developer Portal of Choice WMI - VB 6.0 Windows Management Instrumentation http://www.eggheadcafe.com/tutorials/aspnet/5fb2b0ca-919a-406b-8566-a9d07f76c868/wmi--vb-60-windows-mana.aspx Yes is stored as a -1 (minus one) so ...

Printing a selection of Worksheet
How does one go about printing just the part of a work sheet that has been selected? When I select and then go to print preview, I see a much larger section showing to be printed. Thank you for any help Appreciative Jerry Hi From the Print Preview dialog box, click on 'Selection' -- Andy. "JER" <jer@home> wrote in message news:O6tORZhUEHA.1048@tk2msftngp13.phx.gbl... > How does one go about printing just the part of a work sheet that has been > selected? When I select and then go to print preview, I see a much larger > section showing to be printed. Tha...

Department, Category and Sub Category?
Hi I am just wondering if there is a way round to having sub category in RMS. Example : Department > CPU Category> Intel Sub Category> L775a Chipset At the moment I am using Sub Description 1 etc ..but its not very helpful when searching for the product as the Item Lookup screen doesn not show the department nor the category nor sub description 1 column to search for products! Please if any one has a way round it please do let me know regards Shiri Jnr Dushayant Dhar wrote: > Hi I am just wondering if there is a way round to having sub category in RMS. RMS not have Sub...

selecting a group of records that have a constant average
I have a table that changes daily and contains 2 fields: ID(unique value), Score(with a range of 20 to 200) I want to be able to choose the maximum number of records that will always have an average score of 45. Not an easy problem. It seems a variation of the well known 'back pack" problem, which is a complex algorithm (and generally, people only use an heuristic, giving an approximate solution, rather than the exact algorithm). Indeed, define, for each record, the variable x[i] such that x[i]=0 if we don't pick the record and x[i]=1 if we pick it. We want: Optimize (Ma...

Open Workbook
Looking to write some code that will open a workbook/worksheet and then take a range and use it as a table for a vlookup query ... HAve entered the following Dim OFFICE As Workbook Dim Result As String Dim PERSONNELRange As Range Workbooks.Open ("c:\OFFICE.xls") Worksheets("Personnel").Select PERSONNELRange = Worksheets("Personnel").Range("C2", "D1000").Select Result = Application.WorksheetFunction.VLookup(RegNo, PERSONNELRange, 2, False) msg = MsgBox(Result, vbOKOnly) Not working at all ... can anyone assist with a step for a hint pleas...

Selecting unique record from duplicate
Hi, please help I've just run a 'find duplicates' query on a table. Now I'm trying to select a single unique record from each duplicate. One of my fields is called 'OBSERVER' - this contains either a persons name OR 'unknown' if the persons name is not known. If one of the duplicate records contains a persons name and the other duplicate is 'unknown', how do I select the record with the persons name? If both duplicates contain 'unknown' or both contain a name, i don't mind which record gets selected. See example below... Uni...

Sub: finding a precise value
Hi... I am giving a search for word column name "package" through Cells.Find("Package"). If it finds Package2 ..then also it returns a true value.... Is there a way to precisely find "Package"???? Thanks monika Monika, Don't know how to do it in the find, but you could try this Function FindIt() Dim oFind As Range Dim sFirst As String Dim sFind As String With Columns(1) sFind = "Package" Set oFind = .Find(sFind) sFirst = oFind.Address Do If Not oFind Is Nothing Then If Len(oFind....

how to invoke select names form
hi all... i want to invoke the form(Select Names form that will be opened when we click the To button in the new mail message) programatically in my form ... how can i do this ... I don't think you can as it is not a form but a cache of addresses = previously used in the TO: field - however, you can always ask in a = programming group "down the hall." --=81 Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. All unsolicited mail sent to my personal account will be deleted without reading. =20 After furious head scratching, Malli asked...

Operation of Asian text font selection in the Font dialog
I have a document that has been translated into Japanese, in which all of the translated text appears in MS Gothic font. This displays most of the characters correctly, but not all. Apparently, they are correct if the font is changed to MS PMincho. I'm trying to do this globally by changing the Font settings in the various styles, but no matter what I set in the Asian Text Font and Font boxes, the Japanese text remains stuck on MS Gothic, although any latin text changes to PMincho. I can apply PMincho as a character style, but that's tedious. How do I get the required ...

Selected pages to print from MS Outlook
Is there a way to select the the page to print from MS outlook. I could only print all pages from my mail rather than selected pages Please help ...

selecting text in a document
Hi All, I have a macro that seeks out a particular string of text in a document and then moves down one line and selects the remaining text nin the document and writes it to a variable and then uses it subsequently in the message section of an email. Recently this method has been failing because the text is not being selected. I don't know if the PC was in EST mode a the time but I need the macro to work no matter what. The code I use is as follows Selection.HomeKey wdStory Selection.Find.Execute "Issued at" Selection.MoveDown wdLine...

Filter Sub Report
I have a report with two subreports. I have a form that filters the main report. How do filter one of the sub reports using vb? ex: main report lists projects with their summary. subreport1 lists tasks created by joe subreport2 lists tasks created by mike DoCmd.OpenReport MainReport, acViewPreview, , strFilter, acWindowNormal Wll only filter the main report. How do i filter the main report so that i can show only task1 created by joe ----Project Name------- ----Summary----------- -------task1 created by joe------ -------------------------- -- -CLSWL CLSWL wrote...

selecting data from a list
I'm setting up a worksheet to track/reserve the use of equipment. I have a list of say 20 items. Column A contains a list of dates for the year. Column B should contain a drop down list of the 20 values, except for any values that have already been used in Column C. Column C should contain the same drop down list - anything from the list of items - excluding ones that have already been used in Column B. Is this possible? Any guidance would be appreciated. Thanks. In H1:H20 we have a list off all 20 pieces of equipment In I1 we have the formula =IF(COUNTIF(C:C,H1)...

need to create a list box based on the selection of another list
I've done this in excel using =indirect in the list box. need to repeat in acces.. did have a response about a week ago, but cannot trace the link someone gave me. sorry to have to ask again, but it looked exatly what i wanted - had a new computer at work and they lost all my stored data... thanks what's your question ? -- Wayne Manchester, England. "Gover" wrote: > I've done this in excel using =indirect in the list box. need to repeat in > acces.. did have a response about a week ago, but cannot trace the link > someone gave me. sorry to have t...

Select Open Cash Document Slow Response Help
When my user is working in receiveables she goes to Apply Sales Documents --> Open Cash Documents In here she must review and open various documents. When she both scrolls and chooses select she is experiencing an extremely poor perfomance issue. When she selects SELECT it actually turns her window grey and it appears to hand for mins before returning results. She has a fairly new PC a Dell Optiplex GX520. I have tried this on the server and the response time is slow as well. We do need new memory for our server and are anticipating doing that next week. I'm wondering if the ne...

Select record External Source
Hi: I would like to insert a record and the record is extracted from Excel file in Access 2003. I know I can write the SQL command as: insert into Table1 select * from Table2 But, what's the expression in Table2 so that it can refer to an Excel file source (e.g. The excel file is C:\test.xls)? I try many times but it still fails. Thanks John John, I'm don't think you can write a SQL statement to directly select information from Excel. I would recommend that you first link to the spreadsheet (File - Get External - Link Tables), or if you need to do this programmatically...