English 中文(简体)
Linq多对多?
原标题:Linq many to many?
  • 时间:2010-02-01 12:45:07
  •  标签:
  • asp.net
  • linq

假设我有产品和收据之间的多对多关系。有没有办法直接查询 product.receipts 或 receipt.products 并获得 Iqueryable,而不必引用 join_table?

最佳回答

我假设你正在使用Linq-to-SQL。

答案是“不,但是…”

你可以在Linq-to-SQL中处理多对多关系,但你确实需要有一个代表连接表的单独类型。

如果您只想对模型对象的子属性进行数据绑定,您可以做一些非常简单的事情,比如:-

public partial class Order
{
  public IEnumerable<Product> Products
  {
    get { return Order_Details.Select(x => x.Product); }
  }
}

请注意选择n加一问题,如果您希望在使用订单列表时使用它。

更多信息:http://www.iaingalloway.com/2015/06/many-to-many-relationships-in-linq-to-sql.html这里

此外,还有许多重复的问题:-

LINQ 许多与男性的关系: 解决办法:

如何加载多对多LINQ查询?

还有更多。

问题回答

Linq to Entities和Entity Framework支持多对多关系。我知道Linq to Sql不支持,而我认为(不确定)Linq to NHibernate支持多对多关系。 (我知道NHibernate支持多对多关系)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签