© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
21 replies
popcorn

❔ Anonymous function in LINQ Select

I have a code as following:

class Item {
    int price {get;set;}
}

bool showMax;
bool showMin;

if (showMax) 
  var myLinqQuery = queryGroup.Select(x => new Item(price= x.Max(y => y.Price), otherFields...));
else if (showMin)
  var myLinqQuery = queryGroup.Select(x => new Item(price= x.Min(y => y.Price), otherFields...));
else
  var myLinqQuery = queryGroup.Select(x => new Item(price= x.Average(y => y.Price), otherFields...));
class Item {
    int price {get;set;}
}

bool showMax;
bool showMin;

if (showMax) 
  var myLinqQuery = queryGroup.Select(x => new Item(price= x.Max(y => y.Price), otherFields...));
else if (showMin)
  var myLinqQuery = queryGroup.Select(x => new Item(price= x.Min(y => y.Price), otherFields...));
else
  var myLinqQuery = queryGroup.Select(x => new Item(price= x.Average(y => y.Price), otherFields...));


Is there a way to somehow pass just the LINQ Max/Min/Average function so I don't have to repeat the rest?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Anonymous and lambda function in C#
C#CC# / help
13mo ago
✅ (Linq) Assigning value in OrderBy lambda function
C#CC# / help
4y ago
❔ Anonymous function as method argument?
C#CC# / help
4y ago
How to define an Anonymous type lambda parameter in Linq Expression
C#CC# / help
4y ago