链接到实体:嵌套 .Any()

发布于 2024-10-15 04:42:42 字数 961 浏览 2 评论 0原文

在存储库中,我需要搜索联系人 (c) 的功能,以获取搜索条件 (sc) 中定义的其他功能的集合。表达式函数如下所示:

// 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.   

In a repository I need to search a contact (c)'s features for a collection of other features defined in a searchCriteria (sc). The Expression Func looks like this:

// 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.   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟月 2024-10-22 04:42:42

嗯,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))   

Well, sc.FeaturePattern appears to be a Tuple, which means it's not part of your entity model/EDMX. So LINQ to Entities doesn't know how to translate it to SQL. But you can pretty easily work around that:

// 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))   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文