C#C
C#3y ago
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...));


Is there a way to somehow pass just the LINQ Max/Min/Average function so I don't have to repeat the rest?
Was this page helpful?