For example have this table:
Name | BodyType
----------------
John | 1
Ted | 2
Daniel| 3开发者_开发知识库
George| 4
I my app i check "1" "2" and "3" checkbox. And i should find 3 rows (John, Ted, Daniel) and NOT George. How can i get this query in LINQ? (not use where p.BodyType!=4
)
Use logic OR
var all = dataContext.Users;
foreach (searchParameter in search)
...?
Put the required IDs into a List<int>
and then Contains
:
var bodyTypes = GetBodyTypesFromSearchParameters();
var query = dataContext.Users.Where(user => bodyTypes.Contains(user.BodyType));
Essentially you need to get your parameters into an Enumerable container and then use LINQ's Contains method when iterating through your table with the Where method:
var parameters = new[] { 1, 2, 3};
var list = new[] { new { Name = "John", BodyType = 1 }, new { Name = "Ted", BodyType = 2 }, new { Name = "Daniel", BodyType = 3 }, new { Name = "George", BodyType = 4 } };
var result = list.Where(c => parameters.Contains(c.BodyType));
精彩评论