Home > Mobile >  Group By and Multiple Where Clause in LinQ C#
Group By and Multiple Where Clause in LinQ C#

Time:02-02

My data source has 3 columns CYear (Date Type),Country (100 Countries), and Ceremony. I want to compare the data for two countries (Pakistan & India) based on Ceremony in years 2010-2013

My Query

var que = (from e in dataset  
               where (e.country == "Pakistan" || e.Country == "India" 
               ||e.CYear == 2010 || e.CYear == 2011 || e.Start.Year == 2012)
               select e.Ceremony 
               // Group by for a total number of ceremonies each year
              // in the specific countries
              // to show to the user for better understanding

How can I compare the data for two countries and get the ceremony details i.e. total number of ceremony in these countries for the mentioned year.

I want to show the data in the table for a better understading of the data.

CodePudding user response:

var que = (from e in dataset  
           where (e.country == "Pakistan" || e.Country == "India" )
           && (e.CYear == 2010 || e.CYear == 2011 || e.Start.Year == 2012)
           select e.Ceremony `enter code here`

CodePudding user response:

First fix the where clause, then group by country and year:

var que = (from e in dataset  
               where (
                   (e.country == "Pakistan" || e.Country == "India" ) &&
                   (e.CYear == 2010 || e.CYear == 2011 || e.Start.Year == 2012)
                )
               group e by new {e.Country, e.CYear}
          ).Select(g => new {g.Key.Country, g.Key.Year, Count = g.Count()});  
  • Related