实体框架 - Linq To 实体 - 多对多查询问题
我在查询 Linq To Entities 中的多对多关系时遇到问题。 我基本上尝试使用 Linq 复制此查询:
Select *
FROM Customer
LEFT JOIN CustomerInterest ON Customer.CustomerID = CustomerInterest.CustomerID
LEFT JOIN Interest ON CustomerInterest.InterestID = Interest.InterestID
WHERE Interest.InterestName = 'Football'
我环顾网络并没有真正找到任何合适的示例来说明如何执行此操作。 我得到的最接近的是:
List<Customer> _Customers = (from _LCustomers in _CRM.Customer.Include("CustomerInterest.Interest")
where _LCustomers.CustomerInterest.Any(x => x.Interest.InterestName == "Football")
select _LCustomers).ToList();
问题是,如果客户有多个兴趣,其中之一是“足球”,那么所有兴趣都会被退回。 我还查看了 All() ,它有相反的问题,即只有当他们有一种兴趣并且是足球时才会返回,如果他们有两种兴趣并且其中之一不是足球,则不会返回任何内容。
有人有什么想法吗?
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();
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.
Anyone got any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个,
希望这有帮助,
雷。
Try this,
Hope this helps,
Ray.
我不确定你想获得什么。 具有客户兴趣和兴趣的客户列表? 只需根据客户的兴趣开始查询即可。
但这是非常多余的。 为什么不直接获取匹配的客户兴趣呢?
您仍然可以访问其他信息,而无需显式存储。
I am not sure what you want to obtain. A list of customers with a customer interest and a interest? Just start the query at customer interest.
But this is highly redundant. Why not just get the matching customer interests?
You can still access the other information without needing to store it explicitly.
如果您想保持通用,更好的方法是使用实体 sql[Esql]。 因为 L2E 不支持 linq 查询中的 where 集合。
您不能使用
customer.Interests.Where(interest =>interest.Name =='FootBall')
查询如下所示..
context.CreateQuery(@"SELECT VALUE Customer
FROM Customer WHERE EXISTS( SELECT VALUE FROM CustomerInterest WHERE CustomerInterest.Ineterest.Name = 'FootBall')).Include("兴趣");
希望有帮助!
If you trying to keep it generic, better way is to go with entity sql[Esql]. Coz L2E doesn't support where for collections in linq query.
You cannot use
customer.Interests.Where(interest => interest.Name =='FootBall')
The query would look like this..
context.CreateQuery(@"SELECT VALUE Customer
FROM Customer WHERE EXISTS( SELECT VALUE FROM CustomerInterest WHERE CustomerInterest.Ineterest.Name = 'FootBall')).Include("Interest");
hope it helps!
对于 LINQT 来说就这么多了。 尝试在数据库中使用视图或按照 Deepak N 所说的那样工作。
最好的
That's to much for LINQT. Try use a view at your database or work as Deepak N said.
Best