Skip to content

Instantly share code, notes, and snippets.

@SelvinPL
Last active July 19, 2023 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SelvinPL/75e5627c23b9c6151bb2800cc25ee7a3 to your computer and use it in GitHub Desktop.
Save SelvinPL/75e5627c23b9c6151bb2800cc25ee7a3 to your computer and use it in GitHub Desktop.
SO76722226
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; }
}
<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>&lt;ApplicationData&gt;\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