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?
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:
T
is your IEnumerable type
bool
means it needs to return a boolean value
So, when you do
.Where(l => l.InternalName != String.Empty)
// ^ ^---------- boolean part
// |------------------------------ "T" part
The lambda you pass to Where
can include any normal C# code, for example the &&
operator:
.Where(l => l.Title != string.Empty && l.InternalName != string.Empty)
You can include it in the same where statement with the && operator...
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...
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.
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:
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.Internal NAme != String.Empty)
or
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty && l.Internal NAme != String.Empty)