Linq PredicateBuilder多标准

时间:2012-06-06 10:51:24

标签: linq predicatebuilder multiple-select-query

List<Product> Prs = data.Products
                    .Where(x=> x.ProductColors
                                .Where(y=> y.Color=="blue")
                                .Select(z=> z.ProductID)
                                .Contains(x.ID) && x.ProductColors
                                                     .Where(y=> y.Color== "red")
                                                     .Select(z=> z.ProductID)
                                                     .Contains(x.ID))
                    .ToList(); 

如何PredicateBuilder多标准?

1 个答案:

答案 0 :(得分:3)

如下所示:

var inner = PredicateBuilder.False<Product>();
inner = inner.Or (p => p.Description.Contains ("foo"));
inner = inner.Or (p => p.Description.Contains ("far"));

var outer = PredicateBuilder.True<Product>();
outer = outer.And (p => p.Price > 100);
outer = outer.And (p => p.Price < 1000);
outer = outer.And (inner);

var results = data.Products.AsExpandable().Where(outer)

您可以阅读有关PredicateBuilder here

的更多信息