开发者

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&开发者_如何学Golt;Person> value, 
                                          string name)
{
    return value.Where(x => x.PersonDetails.Where(y => y.Name == name));
}

.. and that doesn't compile.

Any clues, peeps?


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));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜