在lambda EX pressions多个Where子句
问题描述:
我有一个简单的lambda EX pression的是这样的:
I have a simple lambda expression that goes something like this:
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty)
现在,如果我想增加一个where子句前pression,比方说, l.InternalName!=的String.Empty
那么会是什么恩pression是什么?
Now, if I want to add one more where clause to the expression, say, l.InternalName != String.Empty
then what would the expression be?
答
可
x=> x.Lists.Include(l => l.Title)
.Where(l => l.Title != String.Empty && l.InternalName != String.Empty)
或
x=> x.Lists.Include(l => l.Title)
.Where(l => l.Title != String.Empty)
.Where(l => l.InternalName != String.Empty)
当你看其中,
实现,你可以看到它接受 Func键(T,布尔)
;这意味着:
When do you look at Where
implementation, you can see it accepts a Func(T, bool)
; that means:
-
T
是IEnumerable的类型 -
布尔
意味着它需要返回一个布尔值
-
T
is your IEnumerable type -
bool
means it needs to return a boolean value
所以,当你
.Where(l => l.InternalName != String.Empty)
// ^ ^---------- boolean part
// |------------------------------ "T" part