linq to entities:嵌套.Any()

时间:2011-02-01 14:41:55

标签: entity-framework-4 linq-to-entities

在存储库中,我需要搜索联系人(c)的功能,以获取searchCriteria(sc)中定义的其他功能的集合。 Expression Func看起来像这样:

// no Problem from here --->  
c => c.firstName.StartsWith(sc.FirstName)  
  && c.lastName.StartsWith(sc.LastName)  
  && c.addressData.Any(a => a.City.StartsWith(sc.City))  
  && c.addressData.Any(a => a.StreetAddr.StartsWith(sc.Street))  
  && c.addressData.Any(a => a.ZIPCode.StartsWith(sc.ZipCode))  
  && c.visit.Any(v=> v.vStartDate >= sc.VisitTimeIntervalStart)  
  && c.visit.Any(v => v.vStartDate <= sc.VisitTimeIntervalEnd)  
  // <-- to here  but this ->  
  && c.contact2feature.Any( 
      c2f => sc.FeaturePattern.Any(       
         ` fp => fp.Item1.featureID == c2f.feature.featureID))     
// thows:  System.Reflection.TargetInvocationException  
// Inner: Unable to process the type System.Tuple 2[], because it has no known mapping to the value layer.   

1 个答案:

答案 0 :(得分:0)

好吧,sc.FeaturePattern似乎是Tuple,这意味着它不是您的实体模型/ EDMX的一部分。因此,LINQ to Entities不知道如何将其转换为SQL。但你可以很容易地解决这个问题:

// extract scalar values from tuple
var featureIds = sc.FeaturePattern.Select(fp => fp.Item1.featureId);

// now do query
// ...
c => c.firstName.StartsWith(sc.FirstName)  
  && c.lastName.StartsWith(sc.LastName)  
  && c.addressData.Any(a => a.City.StartsWith(sc.City))  
  && c.addressData.Any(a => a.StreetAddr.StartsWith(sc.Street))  
  && c.addressData.Any(a => a.ZIPCode.StartsWith(sc.ZipCode))  
  && c.visit.Any(v=> v.vStartDate >= sc.VisitTimeIntervalStart)  
  && c.visit.Any(v => v.vStartDate <= sc.VisitTimeIntervalEnd)  
  // <-- to here  but this ->  
  && c.contact2feature.Any( 
      c2f => featureIds.Contains(       
          fid => fid == c2f.feature.featureID))