Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由group by vs group join生成的查询_C#_.net_Linq_Linq To Sql - Fatal编程技术网

C# 由group by vs group join生成的查询

C# 由group by vs group join生成的查询,c#,.net,linq,linq-to-sql,C#,.net,Linq,Linq To Sql,我有以下由林克集团提出的声明 from c in Categories join p in Products on c equals p.Category into ps select new { Category = new {c.CategoryID, c.CategoryName}, Products = ps }; 但是,这将生成以下左外部联接查询,并返回所有类别,即使没有关联的产品 SELECT [t0].[CategoryID], [t0].[CategoryName], [t1]

我有以下由林克集团提出的声明

from c in Categories
join p in Products on c equals p.Category into ps
select new { Category = new {c.CategoryID, c.CategoryName}, Products = ps };
但是,这将生成以下左外部联接查询,并返回所有类别,即使没有关联的产品

SELECT [t0].[CategoryID], [t0].[CategoryName], [t1].[ProductID], [t1].[ProductName],     [t1].[SupplierID], [t1].[CategoryID] AS [CategoryID2], [t1].[QuantityPerUnit],   [t1].[UnitPrice], [t1].[UnitsInStock], [t1].[UnitsOnOrder], [t1].[ReorderLevel], [t1].[Discontinued], (
    SELECT COUNT(*)
    FROM [Products] AS [t2]
    WHERE [t0].[CategoryID] = [t2].[CategoryID]
    ) AS [value]
FROM [Categories] AS [t0]
LEFT OUTER JOIN [Products] AS [t1] ON [t0].[CategoryID] = [t1].[CategoryID]
ORDER BY [t0].[CategoryID], [t1].[ProductID]
我真正想要的是只返回那些有关联产品的类别。但是如果我像这样重新编写linq查询:

from c in Categories
join p in Products on c equals p.Category
group p by new {c.CategoryID, c.CategoryName} into ps
select new { Category = ps.Key, Products = ps };
这会给出所需的结果,但会为每个类别生成一个查询:


是否有一种方法可以等效于内部联接和group by,并且仍然只生成一个类似于group联接的查询?

该联接的目的是什么

您的原始查询与此相同:

from c in Categories
select new { Category = new { c.CategoryID, c.CategoryName }, c.Products }
我是不是错过了一些明显的东西

如果您只需要包含产品的类别,请执行以下操作:

from c in Categories
where c.Products.Any()
select new { Category = new { c.CategoryID, c.CategoryName }, c.Products }
或者,如果要展平结果:

from p in Products
select new { p, p.Category.CategoryID, p.Category.CategoryName }
后者将转换为内部联接或外部联接,具体取决于该关系是否可为null。可以按如下方式强制等效的内部联接:

from p in Products
where p.Category != null
select new { p, p.Category.CategoryID, p.Category.CategoryName }

有没有一种方法可以等效于内部联接和group by,并且仍然只生成一个类似group联接的查询


不可以。当您说GroupBy之后是对组元素的非聚合访问时,这是一个以组键作为筛选器的重复查询。

您的实体中有Product.Categories或Category.Products列表吗?我使用Northwind db表编写查询。有Category.Products和Product.Category。类别与产品的关系是1对多。你遗漏了一些东西,但我想这并不是很明显。我需要进行连接,因为我正在运行查询的实际表没有FK关系。我仅以Northwind表为例。我需要加入一个团体,我不想把名单弄平。这就是问题的标题。换句话说,我希望结果返回一个类别及其产品列表,但我不希望包含没有产品的类别。但问题并不是我是否可以在没有组的情况下运行查询,而是我如何通过生成单个内部联接查询来运行组。不过,您的第二个查询是最有用的,我可以使用ps.Any()筛选组,这可以删除没有产品但仍然是外部联接的类别。我想我很好奇为什么我不能生成一个内部连接,而不生成这么多sql查询。谢谢。我在想,一定有一种方法可以得到我想要的结果,而不必先将列表展平,然后分组,但我想我必须分两步来完成。
from p in Products
where p.Category != null
select new { p, p.Category.CategoryID, p.Category.CategoryName }
var queryYouWant =
  from c in Categories 
  join p in Products on c equals p.Category
  select new {Category = c, Product = p};

var result =
  from x in queryYouWant.AsEnumerable()
  group x.Product by x.Category into g
  select new { Category = g.Key, Products = g };