LINQ Query - Help with Group & Sum RRS feed

  • Question

  • Hi 

    I need some help writing a LINQ query:

    Data Structure

    Name UK
    Mon 2
    Tue 4
    Wed 66
    Thu 0
    Fri 1
    Description Blah Blah Text
    Name US
    Mon 2
    Tue 5
    Wed 5
    Thu 4
    Fri 5
    Description Some Text
    Name UK
    Mon 1
    Tue 1
    Wed 1
    Thu 3
    Fri 3


    I want to SELECT NEW, GROUP records based on the Name, then SUM the Mon to Fri fields, and use the first Non-Empty Description 

    Any help would be appreciated?


    • Edited by soledev Sunday, March 23, 2014 8:51 PM
    Sunday, March 23, 2014 8:42 PM


  • Have a try with below LINQ query:

    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()
    If this does not work for you, please let me know.

    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.

    Monday, March 24, 2014 8:17 AM