DavidB
DavidB

Reputation: 2596

LINQ contains not creating a like statement

It's been a while since I've used Entity Frameworks and LINQ. I am attempting to perform an SQL like query. This works fine in a one to many relationship defined via a foreign key in the parent table.

myEntity.Where(me => me.relatedEntity.Name.Contains("a");

This correctly translates to a like in SQL. However when I query a many to many relationship via a junction table it creates an equals statement in SQL.

var name = "bo";
myEntity.Where(me => me.Users.Select(u => u.Name).Contains(name));

Am I missing something obvious?

Thanks

Upvotes: 0

Views: 126

Answers (1)

ocuenca
ocuenca

Reputation: 39366

That's correct, in your first query the source is an string and the second one the source is a collection of strings. I think what you are looking for is this:

var r=myEntity.Where(me => me.Users.Any(u => u.Name.Contains(name)));

If you want as condition that all the users contain some string pattern then use All extension method instead Any.

Upvotes: 1

Related Questions