var searchIds = new List<int>{1,2,3,4,5};
var result = persons.Where(p => p.Locations.Any(l => searchIds.Any(id => l.Id == id)));
Asked
Active
Viewed 403 times
-2

jarlh
- 42,561
- 8
- 45
- 63

Kelum Sampath Edirisinghe
- 1,156
- 12
- 21
-
sorry for my question, what do you mean by "find data by field?" your code above filters persons based on location with the given ids... so what is the question. – Bagus Tesa Oct 07 '19 at 09:50
-
Have a look at: https://stackoverflow.com/questions/41266234/entity-framework-core-in-clause-equivalent – Rand Random Oct 07 '19 at 09:51
-
2@BagusTesa - his problem is probably that this query doesn't translate to SQL and throws an exception with the latest ef core but he is playing the `guess my error` game so he doesn't want to tell you – Rand Random Oct 07 '19 at 09:53
-
It's not clear what you want to achive. Please provide more details. – Andreas Schmidt Oct 07 '19 at 09:57
-
1@RandRandom, thats a bit weird, i never thought ef core would fail for simple `any` query as long as its iqueryable. given the link you have provided deals with a separate query that retrieves the actual ids. op didnt provide clear question and trying to clarify things are deemed as rude, perhaps i can play the `guess my error` too at later date. thank you for the help sir. – Bagus Tesa Oct 07 '19 at 09:58
-
1@RandRandom Are you sure this doesn't translate into SQL? It looks OK to me. – DavidG Oct 07 '19 at 09:59
-
@DavidG - nope, not sure at all as my `guess my error` game remark tries to imply, I am basically guessing what the error could be - but I thought that EFCore cant do it since it would need to translate the passed list into `select IN` maybe it can do it - all I know is that if it would fail to generate SQL the newest EFCore throws and exception to warn about client evaluation - https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client – Rand Random Oct 07 '19 at 10:04
1 Answers
2
Try following :
class Program
{
static void Main(string[] args)
{
List<Person> persons = new List<Person>();
List<int> searchIds = new List<int> { 1, 2, 3, 4, 5 };
List<Person> result = persons.Where(p => p.Locations.Any(l => searchIds.Contains(l.Id))).ToList();
}
}
public class Person
{
public List<Location> Locations { get; set; }
}
public class Location
{
public int Id { get; set; }
}

Rand Random
- 7,300
- 10
- 40
- 88

jdweng
- 33,250
- 2
- 15
- 20