开发者

How search LINQ with many parametrs in one column?

开发者 https://www.devze.com 2023-01-19 08:32 出处:网络
For example have this table: Name | BodyType ---------------- John| 1 Ted| 2 Daniel| 3开发者_开发知识库

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消