2014-12-06 57 views
1

我写了一个扩展方法:扩展方法对于IQueryable的工作不

public static IQueryable<TSource> ConditionalDefaultEmpty<TSource>(this IQueryable<TSource> source, bool condition) 
{ 
    return condition ? source.DefaultIfEmpty() : source ; 
} 

然后我调用的方法,像这样:

var q = from sr in myDb.tblStudentsRegInfos 
     from de in myDb.tblDisciplinesEvents.Where(e => sr.Serial == e.tblStudentsRegInfoRef 
                && e.tblStudentsRegInfoRef == studentRegId 
                && (!forStudent || e.PublishOnInternet) 
                && (!formDate.HasValue || e.RegDate >= formDate) 
                && (!toDate.HasValue || e.RegDate <= toDate)) 
        .ConditionalDefaultEmpty(noPrintAll) 
     join dt in myDb.tblDisciplinesTitles on de.tblDisciplinesTitlesRef equals dt.Serial 
     where sr.Serial == studentRegId 
     group sr by new 
       { 
... 
       } 
       into std 
       select new 
       {.... 
       }; 

但我得到这个错误:

会员访问[专栏名称]不合法....

我该如何解决这个问题?

更新:我明白,EF无法编译到的IQueryable ...

+0

请问,当你不使用你的方法查询工作? – Euphoric 2014-12-06 13:03:22

+0

是的,它的工作非常好。 – 2014-12-06 13:03:50

+0

你可以执行第二个(你的方法的部分)作为单独的查询吗? – Euphoric 2014-12-06 13:04:32

回答

-1

你就不能添加自定义扩展方法,并希望实体框架将能够把它翻译成SQL,尽管这会非常酷,至少对于这样的情况。

这就是说,你可以把IQueryableIEnumerable

.AsEnumerable() 
.ConditionalDefaultEmpty(noPrintAll) 
+0

我不明白downvote,因为这正是原因。也许是因为子查询中的AsEnumerable()不会有帮助,因为EF不能将查询的第一部分翻译成SQL,而是在内存中执行其余部分。 – 2014-12-06 19:06:39