tags:

views:

51

answers:

2

Hi folks,

Update - I fixed the query below. I had the wrong query/error statement :(

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.

+2  A: 

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?

Philip Rieck
He, thanks to your example I just realized that Any can be used like the IN SQL clause.
Edgar Sánchez
Sauce. Of. Awesome. (I also used Any and it returned all results, btw).
Pure.Krome
A: 

U should return bool value in statement:

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

So should do this:

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

the complete code is:

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