如何循环遍历Linq中.select语句中的所有元素

时间:2012-03-14 15:34:50

标签: c# linq

我正在尝试打印“书名,作者姓名”(有些书籍不止一位作者) Linq代码下面只打印第一作者。如何打印与书籍相关的所有作者?

 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 这是xml代码。                      测试1            约翰           DOE               约翰            斯基特      

 <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>

我想要显示的输出如下:(标题):所有那些预订的作者

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

2 个答案:

答案 0 :(得分:4)

目前尚不清楚您是想要多个作者的单个结果,还是每位作者的单个结果,或者您希望如何在List<string>中表示这一结果。我怀疑这会做你需要的事情:

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);
     }
 }

答案 1 :(得分:1)

我选择所有作者进入IEnumerable然后聚合它们:

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();