本文介绍了使用LINQ和实体如何返回多对多关系的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体,帐户和订阅与它们之间的多对多关联。我似乎无法在教程中找到任何地方如何执行以下操作:



我想查找具有类型x的订阅的所有帐户。如果我没有使用Entity框架,我可以加入AccountSubscription表,但是不能通过Entity访问。如果我需要查询多对多的关系,我必须创建一个特殊的实体吗?

解决方案

EF应该创建一种多对多关系的导航属性。那么你应该可以这样做:

  var accounts = from a in Accounts 
where a.Subscriptions .Any(s => s.SubscriptionType ==something)
选择一个;

例如,我有一个简单的数据库,产品和组之间有很多关系:



而EF创建关联在模型中:



所以我可以创建一个这样的查询(这里包括组,所以我可以看到类别):




I have two entities, Account and Subscription with a many-to-many association between them. I can't seem to find in the tutorials anywhere how to do the following:

I want to find all the Accounts with a Subscription of type x. If I wasn't using the Entity framework I could join to the AccountSubscription table, but that isn't accessible via Entity. Do I have to create a special entity if I need to query on a many-to-many relationship?

解决方案

EF should create a navigation property for a many-to-many relationship. Then you should be able to do something like this:

var accounts = from a in Accounts
               where a.Subscriptions.Any(s => s.SubscriptionType == "something")
               select a;

For example, I have a simple db with a many to many relationship between Products and Groups:

And EF creates the association in the model:

So I can create a query like this (here including the Groups so I can see the Category):

这篇关于使用LINQ和实体如何返回多对多关系的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 10:18