Created
November 18, 2022 05:45
-
-
Save ptupitsyn/6389e8a9baa4d8bea8b527bd5560a903 to your computer and use it in GitHub Desktop.
Apache Ignite.NET Dynamic LINQ Expression Query
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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net7.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Apache.Ignite" Version="2.14.0" /> | |
<PackageReference Include="Apache.Ignite.Linq" Version="2.14.0" /> | |
</ItemGroup> | |
</Project> |
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
using System.Linq.Expressions; | |
using Apache.Ignite.Core; | |
using Apache.Ignite.Core.Cache; | |
using Apache.Ignite.Core.Cache.Configuration; | |
using Apache.Ignite.Linq; | |
// Construct "x => x.Value.Seats > 2" expression dynamically. | |
var param = Expression.Parameter(typeof(ICacheEntry<int, Car>), "x"); | |
var entryValueMember = Expression.MakeMemberAccess(param, typeof(ICacheEntry<int, Car>).GetProperty("Value")!); | |
var seatsMember = Expression.MakeMemberAccess(entryValueMember, typeof(Car).GetProperty("Seats")!); | |
var comparand = Expression.Constant(2); | |
var comparison = Expression.GreaterThan(seatsMember, comparand); | |
var lambda = Expression.Lambda<Func<ICacheEntry<int, Car>, bool>>(comparison, param); | |
// Run query. | |
var ignite = Ignition.Start(); | |
var cacheConfiguration = new CacheConfiguration("cars", new QueryEntity(typeof(Car))); | |
var cache = ignite.GetOrCreateCache<int, Car>(cacheConfiguration); | |
cache[1] = new Car("Honda", 4); | |
var query = cache.AsCacheQueryable().Where(lambda); | |
var sql = query.ToCacheQueryable().GetFieldsQuery().Sql; | |
var result = query.ToList(); | |
Console.WriteLine(sql); | |
foreach (var entry in result) | |
{ | |
Console.WriteLine(entry); | |
} | |
public record Car([property: QuerySqlField] string Name, [property: QuerySqlField] int Seats); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment