Last active
July 19, 2023 15:06
-
-
Save SelvinPL/75e5627c23b9c6151bb2800cc25ee7a3 to your computer and use it in GitHub Desktop.
SO76722226
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
GetItems(MathOperation.Average).Dump(); | |
GetItems(MathOperation.Max).Dump(); | |
GetItems(MathOperation.Min).Dump(); | |
} | |
IEnumerable<Item> GetItems(MathOperation mathOperation) | |
{ | |
Expression<Func<IGrouping<long, Album>, decimal>> operation = mathOperation switch | |
{ | |
MathOperation.Max => x => x.Max(y => y.AlbumId), | |
MathOperation.Min => x => x.Min(y => y.AlbumId), | |
MathOperation.Average => x => (decimal)x.Average(y => y.AlbumId), | |
_ => throw new ArgumentOutOfRangeException(), | |
}; | |
return Albums.GroupBy(a => a.ArtistId).AsExpandable().Select(x => new Item | |
{ | |
Artist = x.Key, | |
Something = operation.Invoke(x) | |
}); | |
} | |
enum MathOperation | |
{ | |
Max, | |
Min, | |
Average | |
} | |
class Item | |
{ | |
public long Artist { get; set; } | |
public decimal Something { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Connection> | |
<ID>54bf9502-9daf-4093-88e8-7177c12aaaaa</ID> | |
<NamingService>2</NamingService> | |
<Persist>true</Persist> | |
<Driver Assembly="(internal)" PublicKeyToken="no-strong-name">LINQPad.Drivers.EFCore.DynamicDriver</Driver> | |
<AttachFileName><ApplicationData>\LINQPad\ChinookDemoDb.sqlite</AttachFileName> | |
<DisplayName>Demo database (SQLite)</DisplayName> | |
<DriverData> | |
<PreserveNumeric1>true</PreserveNumeric1> | |
<EFProvider>Microsoft.EntityFrameworkCore.Sqlite</EFProvider> | |
<MapSQLiteDateTimes>true</MapSQLiteDateTimes> | |
<MapSQLiteBooleans>true</MapSQLiteBooleans> | |
</DriverData> | |
</Connection> | |
<NuGetReference>LinqKit</NuGetReference> | |
<Namespace>LinqKit</Namespace> | |
</Query> | |
void Main() | |
{ | |
GetItems(MathOperation.Average).Dump(); | |
GetItems(MathOperation.Max).Dump(); | |
GetItems(MathOperation.Min).Dump(); | |
} | |
IEnumerable<Item> GetItems(MathOperation mathOperation) | |
{ | |
Expression<Func<IGrouping<long, Album>, decimal>> operation = mathOperation switch | |
{ | |
MathOperation.Max => x => x.Max(y => y.AlbumId), | |
MathOperation.Min => x => x.Min(y => y.AlbumId), | |
MathOperation.Average => x => (decimal)x.Average(y => y.AlbumId), | |
_ => throw new ArgumentOutOfRangeException(), | |
}; | |
return Albums.GroupBy(a => a.ArtistId).AsExpandable().Select(x => new Item | |
{ | |
Artist = x.Key, | |
Something = operation.Invoke(x) | |
}); | |
} | |
enum MathOperation | |
{ | |
Max, | |
Min, | |
Average | |
} | |
class Item | |
{ | |
public long Artist { get; set; } | |
public decimal Something { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment