简体   繁体   中英

How can I loop Through all the elements in .select statement in Linq

I am trying to Print "Title of the Book , AuthorNames " (Some Books have more than one author) Below Linq code is printing only first author. How can I print all the authors associated with a book?

 List<string> books = docs.Descendants(name)

   .Select(x => new {
       Title = (string)x.Element(ns+"TITLE"),
           Author = x.Element(ns+"INTEL_AUTH")
        })
.Select(x => new {
           Title = x.Title,
           FirstName = (string) x.Author.Element(ns+"FNAME"),
           MiddleInitial = (string) x.Author.Element(ns+"MNAME"),
           LastName = (string) x.Author.Element(ns+"LNAME"),
        })
.Select(x => string.Format("{0}: {1} {2} {3}",
                           x.Title,
                           x.FirstName, x.MiddleInitial, x.LastName))
.ToList();

for (int i = 0; i < books.Count; i++)
{       
   Response.Write("--" + books[i] + "<BR />" );      
}

XML Here is the xml code. TEST 1 JOHN DOE JOHN SKEET

 <INTEL_AUTH ID="10">
 <FNAME>JO</FNAME>
 <MNAME></MNAME>
 <LNAME>JO</LNAME>
 <INTEL_AUTH>
 </INTEL>

  <INTEL>
  <TILE> LEARNING LINQ</TITLE>
  <INTEL_AUTH ID="2">
  <FNAME> SHELLY</FNAME>
  <MNAME></MNAME>
  <LNAME>SHELLY</LNAME>
  <INTEL_AUTH iD="3">
  <FNAME>CHRIS</FNAME>
  <MNAME></MNAME>
  <LNAME>MADISON</LNAME>
  </INTEL_AUTH>


  </INTEL>
  </RECORD>
  </XML>

I WANT THE OUTPUT TO BE DISPLAYED LIKE : (TITLE) : ALL AUTHORS WHO WROTE THAT BOOK

TEST 1 : JOHN DOE , JOHN SKEET , JO JO LEARNING LINQ : SHELLY SHELLY , CHRIS MADISON

It's not clear whether you want a single result with multiple authors, or a single result per author, or how you'd expect this to be represented in a List<string> . I suspect this will do what you need though:

var query = docs.Descendants(name)
    // First get the relevant elements out of the main book element
    .Select(x => new {
        Title = (string) x.Element(ns + "TITLE"),
        Authors = x.Elements(ns + "INTEL_AUTH")
     })
    // Now transform them from LINQ to XML into plain objects
    .Select(x => new {
        Title = x.Title,
        Authors = x.Authors.Select(a => new {
            FirstName = (string) a.Element(ns + "FNAME"),
            MiddleInitial = (string) a.Element(ns + "MNAME"),
            LastName = (string) a.Element(ns + "LNAME")
        })
     });

 foreach (var book in query)
 {
     Console.WriteLine("{0}:", book.Title);
     foreach (var author in book.Authors)
     {
         Console.WriteLine("  {0} {1} {2}",
                           author.FirstName, author.MiddleInitial,
                           author.LastName);
     }
 }

I select all authors into an IEnumerable then Aggregate them:

var books = docs.Descendants(name)    
                .Select(x => new
                {
                    Title = (string)x.Element(ns + "TITLE"),
                    Author = x.Elements(ns + "INTEL_AUTH") // Elements not Element here
                })
                .Select(x => new
                {
                    Title = x.Title,
                    Names = x.Author.Select(i => new
                    {
                        FirstName = (string)i.Element(ns + "FNAME"),
                        MiddleInitial = (string)i.Element(ns + "MNAME"),
                        LastName = (string)i.Element(ns + "LNAME")
                    })
                })
                .Select(x => string.Format("{0}: {1}",
                                   x.Title,
                                   x.Names
                                        .Select(i => 
                                            string.Format("{0} {1} {2}", 
                                                i.FirstName, 
                                                i.MiddleInitial, 
                                                i.LastName))
                                       .Aggregate((working, next) => 
                                           working + " | " + next)))
                .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM