tags:

views:

33

answers:

2

Hello!

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)
  ...?
+3  A: 

Put the required IDs into a List<int> and then Contains:

var bodyTypes = GetBodyTypesFromSearchParameters();
var query = dataContext.Users.Where(user => bodyTypes.Contains(user.BodyType));
Jon Skeet
damnit you were first :)
Stefanvds
And if i have languages (in one column) Languages: 1,2,3,4;1,2,3. and i need 1,2,3 and not 4 (varchar)
Dmitriy
@Dmitriy: If you've got one column specifying multiple values, I'd say you need to revisit your database design. Implement many-to-many relationships with a mapping table instead. Using a comma-separated list is going to make things really awkward.
Jon Skeet
I know. Unfortunately this is not my DB and I have to work with such ugly design :( thx you
Dmitriy
And if i have table one-to-many: `ID` and second table `UserID|LanguageID`. How can i select OR query?
Dmitriy
@Dmitriy: I'm afraid it's not very clear what you mean just from comments. I suggest you start a new question with a clear explanation instead of trying to squeeze multiple questions into one.
Jon Skeet
http://stackoverflow.com/questions/3871186/how-linq-many-to-many-with-or-search my question
Dmitriy
+1  A: 

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));
Andy Rose