How to make the following Linq / Lambda code?

Note: pseduo code and fake classes / properties branded-in-place ... to protect the innocent

I am trying to get an instance Personwhere a person has a specific name ... as a result IQueryable.

Given the following code ...

public class Person
{
    public ICollection<PersonDetails> PersonDetails { get; set; }
}

public class PersonDetails
{
    public string Name { get; set; }
}

how can i get Personwho has the name "fred"?

I tried (which failed) ....

public static IQueryable<Person> WithName(this IQueryable<Person> value, 
                                          string name)
{
    return value.Where(x => x.PersonDetails.Where(y => y.Name == name));
}

.. and this does not compile.

Any clues, peeps?

+3
source share
1 answer

Try Anyinstead of the second Where:

public static IQueryable<Person> WithName(this IQueryable<Person> value, 
                                          string name)
{
    return value.Where(x => x.PersonDetails.Any(y => y.Name == name));
}
+12
source

Source: https://habr.com/ru/post/1774094/


All Articles