Custom method sometimes translates, sometimes doesn't RRS feed

  • Question

  • I have this function:
    public static IQueryable<Article> WhereArticleIsLive(this IQueryable<Article> q)
    return q.Where(x =>
    != null
    && DateTime.UtcNow >= x.PublishTime
    && x.IsPublished
    && !x.IsDeleted);

    And it works just fine in this query:

    from a in Articles.WhereArticleIsLive()
    where a.Id == 5
    select new { a.Title }

    But it doesn't work in this only slightly more complex query:

    from s in Series
    from a in Articles.WhereArticleIsLive()
    where s.Id == a.SeriesId
    select new { s, a }

    I get this error message:

    NotSupportedException: LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[TheFraser.Data.Articles.Article] WhereArticleIsLive(System.Linq.IQueryable1[TheFraser.Data.Articles.Article])' method, and this method cannot be translated into a store expression.

    Any idea why? Is there another way to consolidate query parameters like this?

    Thanks in advance,


    Monday, August 2, 2010 11:29 PM


  • Are you using EFv1 (.net 3.5) or EFv4 (.net 4)?

    It should work if using EFv4, but EFv1 was a bit more limited on translating linq queries...

    Have you tried an intermediate local variable for the filtered query? E.g.:

    var liveArticles = Articles.WhereArticleIsLive();

    var q =
    from s in Series
    from a in liveArticles
    where s.Id == a.SeriesId
    select new { s, a };

       Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools - add-in with new useful features for the L2S and EF designers in VS2008 and VS2010
    huagati.com/L2SProfiler - Query profiler for Linq-to-SQL, Entity Framework v4, and LLBLGen Pro
    Tuesday, August 3, 2010 2:47 AM