C
C#16mo 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...));
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?
15 Replies
blueberriesiftheywerecats
chat gpt answer
class Item {
int price { get; set; }
}

bool showMax;
bool showMin;

Func<IEnumerable<int>, int> aggregationFunction;

if (showMax)
aggregationFunction = collection => collection.Max();
else if (showMin)
aggregationFunction = collection => collection.Min();
else
aggregationFunction = collection => (int)collection.Average();

var myLinqQuery = queryGroup.Select(x => new Item { price = aggregationFunction(x.Select(y => y.Price)), otherFields = ... });
class Item {
int price { get; set; }
}

bool showMax;
bool showMin;

Func<IEnumerable<int>, int> aggregationFunction;

if (showMax)
aggregationFunction = collection => collection.Max();
else if (showMin)
aggregationFunction = collection => collection.Min();
else
aggregationFunction = collection => (int)collection.Average();

var myLinqQuery = queryGroup.Select(x => new Item { price = aggregationFunction(x.Select(y => y.Price)), otherFields = ... });
Thinker
Thinker16mo ago
Is this EF?
popcorn
popcornOP16mo ago
Yes, EF6
Thinker
Thinker16mo ago
then no, you have to repeat it actually no wait
popcorn
popcornOP16mo ago
The chatgpt won't work? It looks promising
Thinker
Thinker16mo ago
lmao no I have no idea what that does EF can't translate arbitrary function invocations regardless
blueberriesiftheywerecats
maybe, but it might would want to work with queryGroup.ToList(), not just queryGroup
Thinker
Thinker16mo ago
Expression<Func<Item, int>> priceFunc = y => y.Price;
// ...
queryGroup.Select(x => new Item(price = x.Max(priceFunc), otherFields...));
Expression<Func<Item, int>> priceFunc = y => y.Price;
// ...
queryGroup.Select(x => new Item(price = x.Max(priceFunc), otherFields...));
this might work probably not though
blueberriesiftheywerecats
it might
Thinker
Thinker16mo ago
It would work on IEnumerable/List but certainly not on IQueryable Thing is that EF makes it difficult to factor out selectors and whatnot because it has to be able to translate the entire query.
popcorn
popcornOP16mo ago
I cast it to List anyways, so I think it might be okay to just do these Max/Min/Avg on the list instead in the query? Or I can do all 3 at the same time, but I really need just one of these based on the conditions so I was thinking it would cost performace for nothing.
Thinker
Thinker16mo ago
I'm no EF expert but I don't think you want to call .ToList() if you're gonna do more filters/maps on it.
popcorn
popcornOP16mo ago
Yea because after the .ToList() it won't be a SQL Query but just the LINQ filtering which is perhaps slower
Thinker
Thinker16mo ago
won't necessarily be slower, just won't act towards the database .ToList() executes the query synchonously and returns a list.
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server