在 QueryBuilder 中使用 or 运算符连接 where 查询
QueryBuilder 在 Microsoft.Windows.Data.DomainServices 中定义。 它允许您在类型 T 的集合上创建查询并稍后应用它。
当我需要通过 && 连接 where 查询时这很容易,例如你可以做
var query = new QueryBuilder<Customer>();
if (!string.IsNullOrEmpty(this.CustomerFirstName))
query = query.Where(c => c.FirstName == this.CustomerFirstName);
if (!string.IsNullOrEmpty(this.CustomerLastName))
query = query.Where(c => c.LastName == this.CustomerLastName);
if (!string.IsNullOrEmpty(this.CustomerPhone))
query = query.Where(c => c.Phone == this.CustomerPhone);
if (!string.IsNullOrEmpty(this.CustomerMail))
query = query.Where(c => c.Mail == this.CustomerMail);
我不知道我应该如何通过||连接那些Where查询(或者)??
我有一个加载数据库中所有产品的查询,因为产品是按类别组织的,并且用户只能选择类别的子集(他感兴趣的类别) 我只想加载用户指定的类别中的产品
我知道我可以通过 && 连接Where()我排除未选择的类别 举个例子
query.Where(c => c.CategoryName != "MyCategory");
,但我不喜欢它。
我想在 foreach 循环中执行此操作
private void LoadProducts()
{
var query = new QueryBuilder<Product>();
//Get Only Products in specified categories
if (!string.IsNullOrEmpty(WebContext.Current.User.SelectedCategoriesCSV))
{
foreach (string cat in WebContext.Current.User.SelectedCategoriesCSV.Split(';'))
{
????//query.Where(c => c.CategoryName == cat || );
}
}
.....
QueryBuilder is defined in Microsoft.Windows.Data.DomainServices.
It allows you to create a Query on a collection of type T and apply it later.
When I need to concatenate where queries by && it is easy, for example you can do
var query = new QueryBuilder<Customer>();
if (!string.IsNullOrEmpty(this.CustomerFirstName))
query = query.Where(c => c.FirstName == this.CustomerFirstName);
if (!string.IsNullOrEmpty(this.CustomerLastName))
query = query.Where(c => c.LastName == this.CustomerLastName);
if (!string.IsNullOrEmpty(this.CustomerPhone))
query = query.Where(c => c.Phone == this.CustomerPhone);
if (!string.IsNullOrEmpty(this.CustomerMail))
query = query.Where(c => c.Mail == this.CustomerMail);
I can't find out how should I concatenate those Where queries by || (or)??
I have a query that loads all Products in a database, since products are organized by category and a user can select only a sub-set of categories (the one he is interested in)
I would like to load only products in categories that the user has specified
I know I could do it concatenating Where() by && where I exclude unselected categories
example
query.Where(c => c.CategoryName != "MyCategory");
but I don't like it.
I would like to do it in a foreach loop
private void LoadProducts()
{
var query = new QueryBuilder<Product>();
//Get Only Products in specified categories
if (!string.IsNullOrEmpty(WebContext.Current.User.SelectedCategoriesCSV))
{
foreach (string cat in WebContext.Current.User.SelectedCategoriesCSV.Split(';'))
{
????//query.Where(c => c.CategoryName == cat || );
}
}
.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要动态构建 where 表达式,然后将其传递给 query.Where()。查看 PredicateBuilder 提供的 Or 方法来完成此操作。希望这有帮助。
You need to dynamically build your where expression and then pass it once to the query.Where(). Check out the PredicateBuilder that provides Or method to accomplish that. Hope this helps.
您可以使用此链接中提供的动态 LINQ 运算符:
http://msdn.microsoft.com/en-us/bb330936.aspx
(下载c#示例并在\LinqSamples\DynamicQuery目录中获取代码)
You can use Dynamic LINQ operator that are available at this link:
http://msdn.microsoft.com/en-us/bb330936.aspx
(download the c# example and get the code in the \LinqSamples\DynamicQuery directory)
您可以使用Monty's Gush开发的
PredicateBuilder
类。尝试这个。
You can use
PredicateBuilder
class developed by Monty’s Gush.Try this.