.net Linq Where() lambda 表达式中的“或”等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2101540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:49:31  来源:igfitidea点击:

"Or" equivalent in Linq Where() lambda expression

.netlinqwhere-clause

提问by dstr

Is there a method in Linq where you can use to build SQL strings like "...where (a=1) OR (a=2)"?

Linq 中是否有一种方法可以用来构建 SQL 字符串,例如“...where (a=1) OR (a=2)”?

回答by tvanfosson

You can certainly do it within a Where clause (extension method). If you need to build a complex query dynamically, though, you can use a PredicateBuilder.

您当然可以在 Where 子句(扩展方法)中执行此操作。但是,如果您需要动态构建复杂查询,则可以使用PredicateBuilder

 var query = collection.Where( c => c.A == 1 || c.B == 2 );

Or using a PredicateBuilder

或者使用 PredicateBuilder

 var predicate = PredicateBuilder.False<Foo>();
 predicate = predicate.Or( f => f.A == 1 );
 if (allowB)
 {
    predicate = predicate.Or( f => f.B == 1 );
 }

 var query = collection.Where( predicate );

回答by Simon Steele

You can use the standard .NET boolean operators in your single where clause:

您可以在单个 where 子句中使用标准的 .NET 布尔运算符:

MyDataSource.Where(data => data.a == 'a' || data.a == 'b')

回答by Muad'Dib

You use the all the same operators as in normal C# ===> || for "or" && for "and" etc.

您使用与普通 C# 中相同的运算符 ===> || 用于“或”&& 用于“和”等。

var something = from s in mycollection
                where s.something == 32 || 
                      s.somethingelse == 45 
                select s

回答by Alastair Pitts

in your .Where()call use the standard Boolean 'Or' operator, ||.

在您的.Where()通话中使用标准的布尔“或”运算符||.

var query = items.Where(item => (item == 1 || item == 2));

All the Where call does is a Boolean comparison on anything you want, so you can fill it with as much conditional logic as you wish.

Where 调用所做的只是对您想要的任何内容进行布尔比较,因此您可以根据需要使用尽可能多的条件逻辑来填充它。

回答by Bora Ayd?n

If you don' t know parameter count, you can use this:

如果你不知道参数计数,你可以使用这个:

Sample Data

样本数据

var parameters= new List<string>{"a","d"};
var sampledata = new Dictionary<string,string>();
    sampledata["a"] = "A";
    sampledata["b"] = "B";
    sampledata["c"] = "C";
    sampledata["d"] = "D";

Code

代码

var query = sampledata.AsQueryable();
var firstItemKey = sampledata.FirstOrDefault().Key;
var queryresult= sampledata.Where(x => x.Key == firstItemKey).AsQueryable();
foreach (var parameter in parameters.Skip(1))
{
    queryresult=queryresult.Concat(query.Where(x => x.Key == parameter));
}
var result = queryresult.ToList();

回答by JMacor

This is built into .net now, not sure if it previously wasn't. Given an existing Linq query you can add a where clause that takes an array of strings (SearchStrings), and check if any of them match whatever object in the collection you're search. Using ToLower() just makes sure that you avoid case sensitivity in SQL queries.

这现在内置于 .net 中,不确定以前是否不是。给定现有的 Linq 查询,您可以添加一个 where 子句,该子句采用字符串数组 (SearchStrings),并检查它们中的任何一个是否与您要搜索的集合中的任何对象匹配。使用 ToLower() 只是确保您避免在 SQL 查询中区分大小写。

query.Where(i => SearchStrings.Any(s => i.ToLower().Contains(s.ToLower()));

You can do the same thing for an 'and' predicate by matching all the words in the array to the collection's object.

通过将数组中的所有单词与集合的对象相匹配,您可以对“and”谓词执行相同的操作。

query.Where(i => SearchStrings.All(s => i.ToLower().Contains(s.ToLower()));

In this example i correlates to each object in a collection, and s correlates to each string in the SearchStrings array.

在此示例中, i 与集合中的每个对象相关,而 s 与 SearchStrings 数组中的每个字符串相关。