我有一个简单的lambda表达式,如下所示:
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty)
现在,如果我想在表达式中添加一个where子句,比如说l.InternalName != String.Empty
那么表达式是什么?
答案 0 :(得分:102)
可以
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)
当您查看Where
实施时,您会看到它接受Func(T, bool)
;这意味着:
T
是您的IEnumerable类型bool
表示需要返回布尔值所以,当你这样做时
.Where(l => l.InternalName != String.Empty)
// ^ ^---------- boolean part
// |------------------------------ "T" part
答案 1 :(得分:13)
传递给Where
的lambda可以包含任何普通的C#代码,例如&&
运算符:
.Where(l => l.Title != string.Empty && l.InternalName != string.Empty)
答案 2 :(得分:5)
您可以将它包含在与&&amp ;;相同的where语句中操作者...
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty
&& l.InternalName != String.Empty)
您可以使用任何比较运算符(将其视为执行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);
......会带回3和10。
答案 3 :(得分:3)
也许
x=> x.Lists.Include(l => l.Title)
.Where(l => l.Title != string.Empty)
.Where(l => l.InternalName != string.Empty)
你也可以把它放在同一个where子句中:
x=> x.Lists.Include(l => l.Title)
.Where(l => l.Title != string.Empty && l.InternalName != string.Empty)
答案 4 :(得分:2)
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty).Where(l => l.Internal NAme != String.Empty)
或
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty && l.Internal NAme != String.Empty)