How to build a custom mapper for a class with a child list #190
-
I am trying to build a custom mapper. I have posted my simplified classes. I am trying to figure out how to do it using gridify? Ideally would like to use a query like "CustomFields[Key1]=Value1" public class TestEntity
{
public string Name { get; set; }
public List<CustomField> CustomFields { get; set; }
}
public class CustomField
{
public string Key { get; init; }
public string Value { get; set; }
}
[Fact]
public void CustomFieldFilteringTest()
{
var entity = new TestEntity
{
Name = Any.String(),
CustomFields = new CustomFields([
new("Key1", "Value1"),
new("Key2", "Value2")
])
};
var dataSource = new List<TestEntity> { entity }.AsQueryable();
// Using LINQ
var linqResult = dataSource.Where(x =>
x.CustomFields.Any(s => s.Key == "Key1" && s.Value == "Value1")).ToList();
Assert.Single(linqResult);
// HOW TO DO IT USING Gridify ??
// var mapper = new GridifyMapper<TestEntity>();
// mapper.AddMap("CustomFields", x => x.CustomFields.Select(s => s.Key));
//
// var list = dataSource.ApplyFiltering($"CustomFieldKey==Key1", mapper).ToList();
// Assert.Single(list);
// Assert.Equal(entity.Name, list[0].Name);
} |
Beta Was this translation helpful? Give feedback.
Answered by
alirezanet
Aug 3, 2024
Replies: 1 comment 1 reply
-
Hi @moxplod, var gm = new GridifyMapper<TestEntity>()
.AddMap("k", q => q.CustomFields.Select(cf => cf.Key))
.AddMap("v", q => q.CustomFields.Select(cf => cf.Value));
var gridifyResult = dataSource.ApplyFiltering("k=Key1,v=Value1", gm); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
moxplod
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @moxplod,
This should work as expected