0

Don't know where to insert group by what I tried

var filteredResult1 = from s in employeelist
                      group s by s.Department into s
                      where s.totalscore > 0 && s.totalscore <  400
                      select s.employeeID;

With this code I got the error :

Error 2 'System.Linq.IGrouping' does not contain a definition for 'totalscore' and no extension method 'totalscore' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)

My question is, how can I translate select employeeID where totalscore > 0 && totalscore < 400 group by department from employeelist into Linq statement.

Guilhem Prev
  • 979
  • 5
  • 17
Gerald
  • 69
  • 8

1 Answers1

3

You have to put your Linq is this order :

  1. From
  2. Where
  3. GroupBy
  4. Select

In your case you can do it like this :

var filteredResult1 = employeelist.Where(emp => emp.totalscore > 0 && emp.totalscore < 400)
    .GroupBy(emp => emp.Department)
    .Select(grp => grp.Select(emp => emp.employeeID).ToList()).ToList();

With the query syntax :

var filteredResult2 = from emp in employeelist
                      where emp.totalscore > 0 && emp.totalscore < 400
                      group emp by emp.Department into grp
                      from emp in grp
                      select emp.employeeID;

emp represent an Employee object
grp represent a Group (a key and a list of Employee objects)

Guilhem Prev
  • 979
  • 5
  • 17
  • @Gerald This is a Linq query, just a fluent one. But I will try to make with the syntax in your question. – Guilhem Prev Aug 02 '19 at 08:11
  • isn't this lamda syntax ? sorry newbie here. – Gerald Aug 02 '19 at 08:16
  • @Gerald Is it, I never used this syntax for my Linq. The 2 syntax return the same but I personally prefered the lamda. – Guilhem Prev Aug 02 '19 at 08:19
  • could you explain or link me to any article that explain about the emp and grp in the syntax pls. – Gerald Aug 02 '19 at 08:20
  • 1
    @Gerald I added the Linq query syntax (without the .ToList()). The emp or grp are just variable name. emp is always an Employee object and grp is a Group (a key and a list of Employee) – Guilhem Prev Aug 02 '19 at 08:21
  • sorry,i just get into linq recently so lamda is difficult for me to understand although it's the same. – Gerald Aug 02 '19 at 08:22
  • could you please tell me why i don't need grp in this case.var filteredResult4 = from s in employeelist: where s.totalscore > 0 && s.totalscore < 400 select s.totalscore; could you please tell me why i don't need grp in this case. – Gerald Aug 02 '19 at 08:26
  • @Gerald Simply because it will give you the list of totalscore that are between 0 and 400, it will not group by department. – Guilhem Prev Aug 02 '19 at 08:51
  • how do i select first 5 that are between 0 and 400 group by department – Gerald Aug 02 '19 at 09:47
  • @Gerald Take a look at the Microsoft doc for this question (or do a google research, it will be better). There is the method Take in Linq to do this. – Guilhem Prev Aug 02 '19 at 10:26