Click here to Skip to main content
15,909,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone, could anybody help me about
"How to select a field that is not in Group By clause in LINQ"

C#
var ReportData = from a in context.Table1
                                  from b in context.Table2

                                  where a.personelID == b.ID
                                  && a.DateField.Value.Year == 2011
                                  group a by new { a.personelID, b.Name, b.Surname} into grouping
                                  select new
                                  {
                                       // grouping.Key.DateField,
                                      Name= grouping.Key.Name,
                                      Surname= grouping.Key.Surname,
                                      PagaBruto =  grouping.Sum(i => i.Bruto)),


                                  };

I can't select the field "DateField" that is in Table1, I don't want to have this field in Group By, because I will get a wrong result.
THANKS!
Posted

gazmendmehmeti - 20 mins ago
You can do it in this way:
C#
var ReportData = from a in context.Table1
                                  from b in context.Table2 
                                  where a.personelID == b.ID
                                  && a.DateField.Value.Year == 2011
                                  group a by new { a.personelID, b.Name, b.Surname} into grouping
                                  select new
                                  {
                                       // grouping.Key.DateField,
                                      DateField = grouping.First().DateField,
                                      Name= grouping.Key.Name,
                                      Surname= grouping.Key.Surname,
                                      PagaBruto =  grouping.Sum(i => i.Bruto))
                                  };

THANKS!
G.M.
 
Share this answer
 
v2
Comments
Wendelius 28-Dec-11 15:03pm    
Pre tags added
Valdet Zabeli 28-Dec-11 15:29pm    
Ok, you were right, it really works but still couldn't solve my problem
if in table are inserted a date for every month and I want to get all data except those that are not in February i would do:
ReportData = ReportData.Where(a => a.DateField.Value.Month != 2);
it doesn't care about this condition because i have just the first month
If you don't want the DateField in the group by section, then it means that for each grouped row you can have multiple values for the DateField. For these values you must choose what aggregate to use, sum/min/max etc.
 
Share this answer
 
Comments
Valdet Zabeli 28-Dec-11 10:41am    
the only thing that i need is to change data in this query after first initialization. for example:
ReportData = ReportData.Where(a => a.DateField.Value.Month != 1);
Wendelius 28-Dec-11 13:25pm    
Sorry, but now I didn't understand. Can you provide an example of the source data and the desired results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900