本文介绍了实体框架 - 临床到实体 - 多对多查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linq To Entities中查询多对多关系时遇到问题。
我基本上试图用Linq复制这个查询:

I am having problems querying many-to-many relationships in Linq To Entities.I am basically trying to replicate this query using Linq:

Select *
FROM Customer
LEFT JOIN CustomerInterest ON Customer.CustomerID = CustomerInterest.CustomerID
LEFT JOIN Interest ON CustomerInterest.InterestID = Interest.InterestID
WHERE Interest.InterestName = 'Football'

我已经看过网络,并没有真正找到任何合适的例子来做到这一点。我最接近的是:

I have looked around the net and not really found any suitable examples of how to do this. The closest I have got is:

List<Customer> _Customers = (from _LCustomers in _CRM.Customer.Include("CustomerInterest.Interest")
                                  where _LCustomers.CustomerInterest.Any(x => x.Interest.InterestName == "Football")
                                  select _LCustomers).ToList();

这样做的问题是,如果客户有多个兴趣,其中一个是足球然后他们都归还了。我也看过All(),它有逆向问题,即只有有兴趣才返回,如果他们有两个,而且其中一个没有足球没有任何回报。

The problem with this is that if a customer has more than one interest and one of them is "Football" then all of them are returned. I have also looked at All() which has the inverse problem, i.e. will only return if they have one interest and it is football, if they have two and one of them isn't football nothing is returned.

任何人有任何想法?

推荐答案

尝试这个,

var result = from c in ctx.Customer
             from i in c.Interest
             where i.InterestName == "Football"
             select c;

希望这有帮助,

Ray。

这篇关于实体框架 - 临床到实体 - 多对多查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:25