Return input type of generic with type constraint in LINQ to Entities (EF4.1)

前端 未结 4 1294
春和景丽
春和景丽 2021-02-20 18:03

I have a simple extension method for filtering a LINQ IQueryable by tags. I\'m using this with LINQ to Entities with an interface of:

public interface ITaggable
         


        
4条回答
  •  星月不相逢
    2021-02-20 18:22

    I suspect that the problem arises from the call to s.Tags. Because s is a Product, but you're calling ITaggable.Tags, the expression that gets generated looks more like:

    set.Where(s=>((ITaggable)s).Tags.Any(...))
    

    That just confuses Entity Framework. Try this:

    ((IQueryable)set)
        .Where(s=>s.Tags.Any(t=>t.Name.ToLower() == tag.ToLower()))
        .Cast();
    

    Since IQueryable is a covariant interface, this will treat the set as an IQueryable, which should work since your second example basically does exactly the same thing.

提交回复
热议问题