Help me group by linq.


  • XDocument xmlthandongdatviet = XDocument.Load(@"Assets/XMLthandongdatviet.xml");
                List<thandong> thandong = (from query2 in xmlthandongdatviet.Elements("truyen").Elements("thandongdatviet")

                                           select new thandong
                                               page = query2.Element("tieudetrang").Value,
                                               seri = query2.Element("taptruyen").Value,
                                               urltitle = query2.Element("duongdantieude").Value


    I want group by all seri

    Wednesday, April 23, 2014 3:57 AM


  • Hello,

    For the group syntax in LINQ, you can follow below example:

    var result = from t in db.Table30314s
                 group new { t.Name, t.Mon, t.Tue, t.Wed, t.Thu, t.Fri } by t.Name into newt
                 select new
                     Name = newt.Key,
                     Mon = newt.Sum(n => n.Mon),
                     Tue = newt.Sum(n => n.Tue),
                     Wed = newt.Sum(n => n.Wed),
                     Thu = newt.Sum(n => n.Thu),
                     Fri = newt.Sum(n => n.Fri),
                     Description = (from tt in db.Table30314s
                                    where tt.Name == newt.Key && tt.Description != null
                                    select tt.Description).FirstOrDefault()

    This LINQ query groups by t.Name.

    For more information regarding group in LINQ, you can refer to this link:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 24, 2014 1:31 AM