LINQ查询问题

时间:2009-12-02 09:46:47

标签: c# linq-to-sql

以下工作正常:

                    (from e in db.EnquiryAreas
                            from w in db.WorkTypes
                            where
                            w.HumanId != null &&
                            w.SeoPriority > 0 &&
                            e.HumanId != null &&
                            e.SeoPriority > 0 &&
                            db.Enquiries.Where(f => 
                                f.WhereId == e.Id && 
                                f.WhatId == w.Id && 
                                f.EnquiryPublished != null && 
                                f.StatusId != EnquiryMethods.STATUS_INACTIVE &&
                                f.StatusId != EnquiryMethods.STATUS_REMOVED &&
                                f.StatusId != EnquiryMethods.STATUS_REJECTED &&
                                f.StatusId != EnquiryMethods.STATUS_ATTEND
                            ).Any()
                            select
                            new
                            {
                                EnquiryArea = e,
                                WorkType = w
                            });

可是:

               (from e in db.EnquiryAreas
                            from w in db.WorkTypes
                            where
                            w.HumanId != null &&
                            w.SeoPriority > 0 &&
                            e.HumanId != null &&
                            e.SeoPriority > 0 &&
                            EnquiryMethods.BlockOnSite(db.Enquiries.Where(f => f.WhereId == e.Id && f.WhatId == w.Id)).Any()
                            select
                            new
                            {
                                EnquiryArea = e,
                                WorkType = w
                            });

+

   public static IQueryable<Enquiry> BlockOnSite(IQueryable<Enquiry> linq)
    {
        return linq.Where(e => 
            e.EnquiryPublished != null && 
            e.StatusId != STATUS_INACTIVE &&
            e.StatusId != STATUS_REMOVED &&
            e.StatusId != STATUS_REJECTED &&
            e.StatusId != STATUS_ATTEND
        );
    }

我收到以下错误:

  

base {System.SystemException}:{“Method'System.Linq.IQueryable 1[X.Enquiry] BlockOnSite(System.Linq.IQueryable 1 [X.Enquiry])'没有支持的SQL翻译。”}

2 个答案:

答案 0 :(得分:1)

Linq to Sql只将某些方法调用转换为SQL,而你的(BlockOnSite)不是其中之一。因此错误。您的方法使用IQueryable<T>并返回IQueryable<T>这一事实并不会使其变得特别。

答案 1 :(得分:1)

好的,我用以下方法解决了它:

        IQueryable<Enquiry> visibleOnSite = EnquiryMethods.VisibleOnSite(db.Enquiries);

        var combinations = (from e in db.EnquiryAreas
                            from w in db.WorkTypes
                            where
                            w.HumanId != null &&
                            w.SeoPriority > 0 &&
                            e.HumanId != null &&
                            e.SeoPriority > 0 &&
                            visibleOnSite.Where(f => f.WhereId == e.Id && f.WhatId == w.Id).Any()
                            select
                            new
                            {
                                EnquiryArea = e,
                                WorkType = w
                            });