Tôi có hai lớp:C# - Năng động, LINQ từ một danh sách điều kiện
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Product> Product { get; set; }
}
public class Product
{
public string ProductNumber { get; set; }
public string ProductColor { get; set; }
}
Và một ví dụ:
Customer[] c = new Customer[]
{
new Customer()
{
FirstName = "FirstName1",
LastName = "LastName1",
Product = new List<Product>
{
new Product()
{
ProductColor = "ProductColor1",
ProductNumber = "11"
}
}
},
new Customer()
{
FirstName = "FirstName2",
LastName = "LastName2",
Product = new List<Product>
{
new Product()
{
ProductColor = "ProductColor2",
ProductNumber = "12"
}
}
}
};
tôi sử dụng System.Dynamic.Linq
thư viện để lọc mảng của tôi:
var filter = c.Where("FirstName == \"FirstName1\"").Select(x => x).ToList();
Tôi tạo một lớp condition
và một phiên bản:
public class condition
{
public string propertyName { get; set; }
public string propertyValue { get; set; }
}
List<condition> f = new List<condition>
{
new condition()
{
propertyName = "FirstName",
propertyValue = "FirstName1"
},
new condition()
{
propertyName = "Product.ProductColor",
propertyValue = "11"
}
};
Tôi muốn tạo ra nhiều khoản Where
từ này List<Condition>
Làm thế nào để làm điều này?
Bạn có thể hiển thị mã nơi bạn hiện đang áp dụng một điều kiện cho danh sách không? Nó phải đơn giản như 'var toFilter = sourceCollection.AsEnumerable(); foreach (điều kiện var trong điều kiện) {toFilter = toFilter.Where (điều kiện); } '. – CodeCaster
Bạn có tất cả các loại proeprties typeof? –
Không, tất cả các thuộc tính không phải là loại chuỗi –