Skip to content

Example #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pazicb opened this issue Mar 29, 2025 · 1 comment
Open

Example #140

pazicb opened this issue Mar 29, 2025 · 1 comment
Assignees

Comments

@pazicb
Copy link

pazicb commented Mar 29, 2025

Hello,
I would need a simple example

I have

List<Item> items = new List<Item>
{
    new Item { Id = 1, Name = "Apple", Price = 50 },
    new Item { Id = 2, Name = "Banana", Price = 120 },
    new Item { Id = 3, Name = "Cherry", Price = 80 },
    new Item { Id = 4, Name = "Date", Price = 200 }
};

and

Expression<Func<Item, bool>> query = item => item.Price > 100;

I would need to serialize this function using System.Text.Json.
Then deserialize it and apply it to items.

Thank you.

@6bee 6bee self-assigned this Mar 30, 2025
@6bee
Copy link
Owner

6bee commented Mar 30, 2025

For your specific request:

// <PackageReference Include="Remote.Linq.Text.Json" Version="7.2.2" />

using Remote.Linq;
using Remote.Linq.Text.Json;
using System.Text.Json;

System.Linq.Expressions.Expression<Func<Item, bool>> query = item => item.Price > 100;

Remote.Linq.Expressions.LambdaExpression remoteExpression = query.ToRemoteLinqExpression();

JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions().ConfigureRemoteLinq();

string json = JsonSerializer.Serialize(remoteExpression, jsonSerializerOptions);

Remote.Linq.Expressions.LambdaExpression deserializedRemoteExpression = JsonSerializer.Deserialize<Remote.Linq.Expressions.LambdaExpression>(json, jsonSerializerOptions);

System.Linq.Expressions.Expression<Func<Item, bool>> filterExpression = deserializedRemoteExpression.ToLinqExpression<Item, bool>();

List<Item> items = new List<Item>
{
    new Item { Id = 1, Name = "Apple", Price = 50 },
    new Item { Id = 2, Name = "Banana", Price = 120 },
    new Item { Id = 3, Name = "Cherry", Price = 80 },
    new Item { Id = 4, Name = "Date", Price = 200 }
};

List<Item> filteredList = items.AsQueryable().Where(filterExpression).ToList();

public record class Item
{
    public required int Id { get; init; }
    public required string Name { get; init; }
    public int? Price { get; init; }
}

More samples can be found in the samples folder and Remote.Linq.Samples.sln respectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants