开发者

Having problems with this Linq/Lambda statement :(

开发者 https://www.devze.com 2023-01-19 04:17 出处:网络
Update - I fixed the query below. I had the wrong query/error stat开发者_Python百科ement :( I have the following statement:

Update - I fixed the query below. I had the wrong query/error stat开发者_Python百科ement :(

I have the following statement:

var posts = BlogPostRepository.Find()
    .Where(x => x.Tags.Where(y => y.Name == tag))
    .ToList();

It's giving me a compile time error with the 2nd (inner) Where clause, saying :-

Error 1 Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type

I'm trying to filter all BlogPosts by a specific tag name.


This part:

  x.Tags.Where(y => y.Name == tag)

will return an IEnumerable of whatever is in Tags that have Name == tag. You are then comparing that to "true" which doesn't make much sense.

Perhaps you want this?

var posts = BlogPostRepository.Find()
    .Where(x => x.Tags.Any(y => y.Name == tag))
    .ToList()

or instead of Any, All?

0

精彩评论

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