使用Dynamic Linq 3.5库,我试图动态过滤这样的对象列表:
public class Field
{
public string Name { get; set; }
public object Value { get; set; }
}
public class Product
{
public string Name { get; set; }
public int Rating { get; set; }
public List<Field> Fields { get; set; }
}
public class Products :
SortableBindingList<Product>
,ITypedList
//,IEnumerable
//,IQueryable
{
..... // irrelevant, just a list
}
Products prods = new Products{
new Product {
Name = "abc",
Rating = 3,
Fields = new List<Field>{
new Field { Name = "One", Value = "One_value" },
new Field { Name = "Two", Value = "Two_value" }
}
},
new Product {
Name = "def",
Rating = 4,
Fields = new List<Field>{
new Field { Name = "Three", Value = "Three_value" },
new Field { Name = "Four", Value = 123 },
new Field { Name = "Two", Value = "Also Two value" }
}
},
new Product {
Name = "ghy",
Rating = 4,
Fields = new List<Field>{
new Field { Name = "Three", Value = "Three_value" },
new Field { Name = "Four", Value = 12 },
new Field { Name = "Two", Value = "12" }
}
},
new Product {
Name = "ghy",
Rating = 4,
Fields = new List<Field>{
new Field { Name = "Three", Value = "Three_value" },
new Field { Name = "Four", Value = 9 },
new Field { Name = "Two", Value = "123" }
}
},
new Product {
Name = "ghy",
Rating = 4,
Fields = new List<Field>{
new Field { Name = "Three", Value = "Three_value" },
new Field{ Name = "Four", Value = 22 },
new Field { Name = "Two", Value = "22" },
new Field { Name = "Five", Value = "Five value" }
}
},
};
现在使用有效的过滤器
var queryable = prods.AsQueryable();
var test = queryable.Where("Name.Contains(\"abc\") && Fields[0].Name.StartsWith(\"One\")").ToProducts();
dgv.DataSource = test;
问题在于字段是对象的列表,我还需要按其名称和值进行过滤。
我只是不知道如何使用值而不是Field [0]来引用过滤器语法中的嵌套属性。
var queryable = prods.AsQueryable();
var test = queryable.Where("Name.Contains(\"139\").Where(Field.Name ==\"One\").Any()").ToProducts(); // not working
谢谢!