LINQ 中的简单查询

发布于 2024-11-01 05:15:07 字数 291 浏览 2 评论 0原文

我有3张桌子。基本上,我有这样的结构:

Customer
--------
IDCustomer
NameCustomer

Product  
«««««««  
IDProduct  
NameProduct

Order  
«««««  
IDCustomer  
IDProduct

如何使用 LINQ 获得这样的结果:

Result  
««««««  
NameCustomer  
NameProduct

提前致谢!

I have 3 tables. Basically, I have this structure:

Customer
--------
IDCustomer
NameCustomer

Product  
«««««««  
IDProduct  
NameProduct

Order  
«««««  
IDCustomer  
IDProduct

How to get a result like this, using LINQ:

Result  
««««««  
NameCustomer  
NameProduct

Thanks in advance!

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

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

发布评论

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

评论(3

呆萌少年 2024-11-08 05:15:07

假设您没有在数据库中设置正确的关系(或者您没有为关系生成导航属性),您可以执行以下操作:

var result = from c in _context.Customer
             join o in _context.Order on c.IDCustomer equals o.IDCustomer
             join p in _context.Product on o.IDProduct equals p.IDProduct
             select new { c.NameCustomer, p.NameProduct }

不过,将正确的外键添加到数据库会更容易并允许实体框架(或 LINQ to SQL)为您生成导航属性。如果是这样的话,那就很简单了:

var result = _context.Order.Select(o => new 
    { 
        o.Customer.NameCustomer,
        o.Product.NameProduct
    });

Assuming you don't have proper relationships set up in the database (or you haven't generated navigation properties for your relationships), you could do the following:

var result = from c in _context.Customer
             join o in _context.Order on c.IDCustomer equals o.IDCustomer
             join p in _context.Product on o.IDProduct equals p.IDProduct
             select new { c.NameCustomer, p.NameProduct }

It would be much easier, though, to add the proper foreign keys to the database and allow Entity Framework (or LINQ to SQL) generate the navigation properties for you. If that were the case, it would be as easy as:

var result = _context.Order.Select(o => new 
    { 
        o.Customer.NameCustomer,
        o.Product.NameProduct
    });
半夏半凉 2024-11-08 05:15:07

LINQ 的全部优点在于,这一切都应该通过自动生成的对象自动为您完成。

在将表关系移动到 DBML 文件之前,请确保在 SQL Server 中设置表关系。此后,您的客户表将附加订单,其中您的产品将附加到您的订单。

例如,从客户表 (LINQ) 中进行选择将为您提供一个客户列表,其中每个客户都会附加一个 IEnumerable 订单对象列表。

The whole beauty of LINQ is that this should be all automatically done for you with automatically generated objects.

Make sure your table relationships are setup in SQL Server before moving them to your DBML file. After this, your Customer table will have Orders attached to it, in which your Products will be attached to your orders.

Such as selecting from your customer table (LINQ) will give you a list of Customers, in which will have an IENumerable list of Order objects attached to each.

临风闻羌笛 2024-11-08 05:15:07
from t1 in Product
join t2 in Order on t1.IDProduct equals t2.IDProduct
join t3 in Customer on t2.IDCustomer equals t3.IDCustomer
select new { t1.NameProduct, t3.NameCustomer}

您可以使用 linqpad.exe 直接针对数据库测试 linq 查询

from t1 in Product
join t2 in Order on t1.IDProduct equals t2.IDProduct
join t3 in Customer on t2.IDCustomer equals t3.IDCustomer
select new { t1.NameProduct, t3.NameCustomer}

you can use linqpad.exe to test your linq query directly against your database

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