2

I have a Linq query like this

var results= StudentsList.GroupBy(x=> x.GroupID)
    .GroupBy(x=> x.Any(g=>g.IsQualified== true))
    .Select(g=> g)
    .ToList();

I want to store the part x.Any(g=>g.IsQualified== true) into a variable so that I can change it on the fly (example: x.Any(g=>g.StudentName== "John") ) based on my requirement and without defining a new Linq query separately. Is that possible?

Pseudo Code

static void SomeFunction(Func<int, int> op)
  {
        var results= StudentsList.GroupBy(x=> x.GroupID)
            .GroupBy(x=> op))
            .Select(g=> g)
            .ToList();
  }

And call it:

SomeFunction(x => x.Any(g=>g.IsQualified== true));
SomeFunction(x => x.Any(g=>g.StudentName== "John"));
SomeFunction(x => x.Any(g=>g.Country== "USA"));
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • 2
    You'd want a `Func, bool>` where I'm assuming that `GroupID` is `int` and the `StudentList` is a collection of `Student` objects and that you always want the second grouping to be on a `bool` value. And uses it like `GroupBy(op)`. Thought if it's always going to be `x => x.Any(...)` you might want to pass in the predicate for `Any` instead. – juharr Feb 23 '20 at 05:00
  • 5
    Look at the method signature of the GroupBy function, just make your SomeFunction take in the exact time. It will be either `Expression>` if you are working with a IQueryable or `Func` if you are working with a IEnumerable. – Scott Chamberlain Feb 23 '20 at 05:04

1 Answers1

0

Demo on dotnet fiddle

Solution 1

You can use Func<StudentInfo, bool> to achieve it.

private static IEnumerable<IGrouping<int, StudentInfo>>  SomeFunction(List<StudentInfo> list, Func<StudentInfo, bool> selector)
{
    return list.GroupBy(x => x.GroupID)
                              .Where(g => g.Any(selector) )
                              .Select(g => g);
}

How to use it?

var result1 = SomeFunction(StudentsList, p => p.IsQualified == true);
var result2 = SomeFunction(StudentsList, p => p.Student == "Adam");

Solution 2 (Create Extension method)

public static IEnumerable<IGrouping<int, StudentInfo>> ExtensionMethod_SomeFunction(this IEnumerable<StudentInfo> list, Func<StudentInfo, bool> selector) 
{
    return list.GroupBy(x => x.GroupID)
                              .Where(g => g.Any(selector) )
                              .Select(g => g);
}

How to use it?

var result3 = StudentsList.ExtensionMethod_SomeFunction(p => p.IsQualified == true);
var result4 = StudentsList.ExtensionMethod_SomeFunction(p => p.Student == "John");
Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
  • 1
    You're my Guru! –  Feb 23 '20 at 06:17
  • I get this error `... not all code paths return a value` when I 1) Make the IEnumerable non-static and 2) Use `.ToList();` at the end to update the collection, instead of returning items with `return list.GroupBy....` –  Feb 23 '20 at 16:46
  • @YoKidYo https://chat.stackoverflow.com/rooms/208379/yokidyo – Nguyễn Văn Phong Feb 24 '20 at 01:01
  • Hi @Phong, would you like to contribute here? https://stackoverflow.com/questions/60635036/issue-with-custom-styled-progress-bar –  Mar 11 '20 at 11:37