代码之家  ›  专栏  ›  技术社区  ›  Niels Bosma

LINQ查询有问题

  •  1
  • Niels Bosma  · 技术社区  · 15 年前

    以下工作正常:

                        (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
            );
        }
    

    我得到以下错误:

    基System.SystemException:“方法”System.Linq.iQueryable 1[X.Enquiry] BlockOnSite(System.Linq.IQueryable 1[x.enquiry])'不支持转换为SQL。“

    2 回复  |  直到 14 年前
        1
  •  1
  •   bruno conde    15 年前

    linq to sql只将某些方法调用转换为sql,而您的方法调用( BlockOnSite )不是他们中的一个。因此出现了错误。你的方法需要 IQueryable<T> 返回一个 可查询 这并不特别。

        2
  •  1
  •   Niels Bosma    15 年前

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

            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
                                });