2009-12-02 5 views
1

Les travaux suivants fin:problème avec requête LINQ

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

Mais:

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

J'obtiens l'erreur suivante:

base {System.SystemException}: {"Method 'System.Linq.IQueryable 1[X.Enquiry] BlockOnSite(System.Linq.IQueryable 1[X.Enquiry])' has no supported translation to SQL."}

+1

Avez-vous Google le message d'erreur? C'est assez descriptif. –

+1

Consultez cette question connexe http://stackoverflow.com/questions/332670/simple-linq-to-sql-has-no-supported-translation-to-sql. Bruno a oublié de coller ceci en copiant sa réponse. –

+0

Je comprends le message d'erreur, mais pas comment je peux réécrire cela afin qu'il fonctionne avec le type de fonction BlockOnSite. –

Répondre

1

Linq to Sql ne traduit que certains appels de méthode vers SQL, et le vôtre (BlockOnSite) n'en fait pas partie. D'où l'erreur. Le fait que votre méthode prenne un IQueryable<T> et renvoie un IQueryable<T> ne le rend pas spécial.

1

Ok je l'ai résolu en utilisant:

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