C# 林克。从多个表中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12339737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq. Select from multiple tables
提问by Alexander Shlenchack
In project I have this tables:
在项目中我有这个表:
- Product(id,catalogId, manufacturerId...)
- Catalog
- Manufacturer
- 产品(id,catalogId,manufacturerId...)
- 目录
- 制造商
Also Productmodel (id, name, catalogId, catalogTitle, manufacturerId, manufacturerName).
还有Product模型(id, name, catalogId, catalogTitle, manufacturerId, manufacturerName)。
How can write in Linq this SQL query below if I want get Product item?
如果我想获取产品项目,如何在 Linq 中编写下面的 SQL 查询?
SELECT Product.Name, Product.CatalogId, Product.ManufacturerId, [Catalog].Name, Manufacturer.Name
FROM Product, [Catalog], Manufacturer
WHERE [Catalog].Id=Product.CatalogId AND Manufacturer.id=Product.ManufacturerId AND Product.Active=1
采纳答案by Jaime Torres
First, I'll answer your question.. then address your answer to comments. To answer your question, in Linq you would do the following:
首先,我会回答你的问题..然后解决你对评论的回答。要回答您的问题,在 Linq 中您将执行以下操作:
from p in Product
join c in Catalog on c.Id equals p.CatalogId
join m in Manufacturer on m.Id equals p.ManufacturerId
where p.Active == 1
select new { Name = p.Name, CatalogId = p.CatalogId, ManufacturerId = p.ManufacturerId, CatalogName = c.Name, ManufacturerName = m.Name };
This will give you an anonymous object with the items you requested. If you need to use this elsewhere (and you're not using dynamic objects), I would suggest creating a view-model, and instantiating one of those in your select.
这将为您提供一个带有您请求的项目的匿名对象。如果您需要在其他地方使用它(并且您没有使用动态对象),我建议您创建一个视图模型,并在您的选择中实例化其中之一。
Example:
例子:
public class ProductInfoView
{
public string Name { get; set; }
public int CatalogId { get; set; }
public int ManufacturerId { get; set; }
public string CatalogName { get; set; }
public string ManufacturerName { get; set; }
}
from p in Product
join c in Catalog on c.Id equals p.CatalogId
join m in Manufacturer on m.Id equals p.ManufacturerId
where p.Active == 1
select new ProductInfoView() { Name = p.Name, CatalogId = p.CatalogId, ManufacturerId = p.ManufacturerId, CatalogName = c.Name, ManufacturerName = m.Name };
This will make referencing your query results a little less painful.
这将使引用您的查询结果不那么痛苦。
To answer your comment, you're doing a lot of joins if all you want is the product. Your criteria will only ensure three things
为了回答您的评论,如果您想要的只是产品,那么您正在做很多连接。你的标准只会确保三件事
- Your product's Active flag is 1
- Your product has an existing Catalog entry
- Your product has an existing Manufacturer entry
- 您产品的活动标志为 1
- 您的产品有一个现有的目录条目
- 您的产品有一个现有的制造商条目
If #2 and #3 are superfluous and you don't necessarily need the names, you could simply do:
如果 #2 和 #3 是多余的并且您不一定需要名称,您可以简单地执行以下操作:
from p in Product
where p.Active == 1
select p
If Product is a CRUD model, you could potentially deep-load it to include Manufacturer/Catalog information, or use the aforementioned view-model.
如果 Product 是一个 CRUD 模型,您可能会深度加载它以包含制造商/目录信息,或者使用上述视图模型。
Good luck!
祝你好运!
回答by Paul Nakitare
To combine results from multiple tables without explicitly joins:
要在不显式连接的情况下组合来自多个表的结果:
from p in Product
from c in Catalog
from m in Manufacturer
where c.Id == p.CatalogId && m.Id == p.ManufacturerId && p.Active == 1
select new
{
p.Name,
p.CatalogId,
p.ManufacturerId,
c.Name,
m.Name
};

