Lambda表达式中的多个Where子句

时间:2022-05-23 01:30:15

I have a simple lambda expression that goes something like this:

我有一个简单的,像这样的表达式

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty)

Now, if I want to add one more where clause to the expression, say, l.InternalName != String.Empty then what would the expression be?

现在,如果我想在表达式中再加一个where子句,比如l。InternalName ! =字符串。那么空的表达式是什么呢?

5 个解决方案

#1


91  

Can be

可以

x => x.Lists.Include(l => l.Title)
     .Where(l => l.Title != String.Empty && l.InternalName != String.Empty)

or

x => x.Lists.Include(l => l.Title)
     .Where(l => l.Title != String.Empty)
     .Where(l => l.InternalName != String.Empty)

When you are looking at Where implementation, you can see it accepts a Func(T, bool); that means:

当你观察实现的位置时,你可以看到它接受Func(T, bool);这意味着:

  • T is your IEnumerable type
  • T是你的IEnumerable类型
  • bool means it needs to return a boolean value
  • bool意味着它需要返回一个布尔值

So, when you do

所以,当你做的事

.Where(l => l.InternalName != String.Empty)
//     ^                   ^---------- boolean part
//     |------------------------------ "T" part

#2


11  

The lambda you pass to Where can include any normal C# code, for example the && operator:

您传递到的lambda可以包含任何正常的c#代码,例如&&操作符:

.Where(l => l.Title != string.Empty && l.InternalName != string.Empty)

#3


5  

You can include it in the same where statement with the && operator...

您可以将它包含在与&&操作符的where语句中。

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty 
    && l.InternalName != String.Empty)

You can use any of the comparison operators (think of it like doing an if statement) such as...

您可以使用任何比较运算符(可以将其视为执行if语句),例如……

List<Int32> nums = new List<int>();

nums.Add(3);
nums.Add(10);
nums.Add(5);

var results = nums.Where(x => x == 3 || x == 10);

...would bring back 3 and 10.

…会得到3和10。

#4


3  

Maybe

也许

x=> x.Lists.Include(l => l.Title)
    .Where(l => l.Title != string.Empty)
    .Where(l => l.InternalName != string.Empty)

?

吗?

You can probably also put it in the same where clause:

你也可以把它放在同一个where子句中:

x=> x.Lists.Include(l => l.Title)
    .Where(l => l.Title != string.Empty && l.InternalName != string.Empty)

#5


2  

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty).Where(l => l.Internal NAme != String.Empty)

or

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty && l.Internal NAme != String.Empty)

#1


91  

Can be

可以

x => x.Lists.Include(l => l.Title)
     .Where(l => l.Title != String.Empty && l.InternalName != String.Empty)

or

x => x.Lists.Include(l => l.Title)
     .Where(l => l.Title != String.Empty)
     .Where(l => l.InternalName != String.Empty)

When you are looking at Where implementation, you can see it accepts a Func(T, bool); that means:

当你观察实现的位置时,你可以看到它接受Func(T, bool);这意味着:

  • T is your IEnumerable type
  • T是你的IEnumerable类型
  • bool means it needs to return a boolean value
  • bool意味着它需要返回一个布尔值

So, when you do

所以,当你做的事

.Where(l => l.InternalName != String.Empty)
//     ^                   ^---------- boolean part
//     |------------------------------ "T" part

#2


11  

The lambda you pass to Where can include any normal C# code, for example the && operator:

您传递到的lambda可以包含任何正常的c#代码,例如&&操作符:

.Where(l => l.Title != string.Empty && l.InternalName != string.Empty)

#3


5  

You can include it in the same where statement with the && operator...

您可以将它包含在与&&操作符的where语句中。

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty 
    && l.InternalName != String.Empty)

You can use any of the comparison operators (think of it like doing an if statement) such as...

您可以使用任何比较运算符(可以将其视为执行if语句),例如……

List<Int32> nums = new List<int>();

nums.Add(3);
nums.Add(10);
nums.Add(5);

var results = nums.Where(x => x == 3 || x == 10);

...would bring back 3 and 10.

…会得到3和10。

#4


3  

Maybe

也许

x=> x.Lists.Include(l => l.Title)
    .Where(l => l.Title != string.Empty)
    .Where(l => l.InternalName != string.Empty)

?

吗?

You can probably also put it in the same where clause:

你也可以把它放在同一个where子句中:

x=> x.Lists.Include(l => l.Title)
    .Where(l => l.Title != string.Empty && l.InternalName != string.Empty)

#5


2  

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty).Where(l => l.Internal NAme != String.Empty)

or

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty && l.Internal NAme != String.Empty)