Sagar Joshi
Sagar Joshi

Reputation: 582

Get Sum of Property from Generic list having another generic list

I have below class

public class SearchResponse
{
       public string SearchTerm { get; set; }
       public List<DataClass> lstDataClass
       public string Category { get; set; }

}

public DataClass
{
        public String Date { get; set; }
        public string NoOfRecord { get; set; }
}

I will be having List<SearchResponse> and I want to have sum of NoOfRecord by grouping on Searchterm of SearchResponse class and Date of DataClass

Suppose

SearchResponse1
{
     SearchTerm="A";
    Category="cata";
     ListOfDataClass
    {
         dataclass
          {
             5 may 2013,
             50
          }
         dataclass
         {
              6 may 2013,
              68
         }
     }
}


SearchResponse2
{
     SearchTerm="A";
    Category="catb";
     ListOfDataClass
    {
         dataclass
          {
             5 may 2013,
             52
          }
         dataclass
         {
              6 may 2013,
              63
         }
     }
}


SearchResponse3
{
     SearchTerm="B";
    Category="catc";
     ListOfDataClass
    {
         dataclass
          {
             5 may 2013,
             48
          }
         dataclass
         {
              6 may 2013,
              21
         }
     }
}

I want

SearchTerm="A", Date=5 May 2013, TotalRecords=102
SearchTerm="A", Date=6 May 2013, TotalRecords=131
SearchTerm="B", Date=6 May 2013, TotalRecords=48
.
.
.

Upvotes: 0

Views: 329

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109118

A linq solution is this:

from x in
(
    from sr in SearchResponse
    from dc in sr.DataClass
    select new { sr.SearchTerm , dc }
)
group x by new { x.SearchTerm, x.dc.Date } into g
select new {
               g.Key.SearchTerm, 
               g.Key.Date, 
               TotalRecords = g.Sum(g1 => g1.p.NoOfRecord) 
           }

which first creates a flat list (new { sr.SearchTerm , dc }) in which Searchterm and Date can be grouped together. Subsequently, in each group the Sum is calculated.

Upvotes: 1

Garrison Neely
Garrison Neely

Reputation: 3289

I think this can be improved, but this is what I had time to do:

var searchTerms = searchResponseList.GroupBy(g => g.SearchTerm)
                .Select(x => x.Key);

            foreach (var searchTerm in searchTerms)
            {
                var matchingDataClasses = searchResponseList.Where(response => response.SearchTerm == searchTerm)
                    .SelectMany(response => response.lstDataClass);

                // Output the data
                var data = matchingDataClasses.GroupBy(g => g.Date)
                    .Select(x => new
                    {
                        SearchTerm = searchTerm,
                        Date = x.Key,
                        TotalRecords = x.Sum(dataClass => dataClass.NoOfRecord)
                    });
            }

One thing in your class is that NoOfRecord needs to change from a string to an int to make the x.Sum() work.

Upvotes: 0

Related Questions