Pure.Krome
Pure.Krome

Reputation: 86947

How do I do the following Linq / Lambda code?

Note: pseduo code and fake-thought-up-on-the-spot classes/properties ... to protect the innocent

I'm trying to retrieve the Person instance, where the person has a particular name ... as an IQueryable result.

Given the following code...

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

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

how can I retrieve a Person, who has the name 'Fred' ?

I was trying (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 that doesn't compile.

Any clues, peeps?

Upvotes: 3

Views: 138

Answers (1)

Mark Byers
Mark Byers

Reputation: 838076

Try Any instead 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));
}

Upvotes: 12

Related Questions