使用linq标准合并两个表

时间:2013-08-01 21:37:35

标签: c# linq

我试图用一个linq句子解决问题,我不知道是否有可能做到这一点。 我有一个名为PRICES的表,其中包含以下字段:

 pkey: int
 region: int?
 product_type: int
 product_size: int
 price: double
 desc: string

唯一键是:product_type + product_size

我想做一个返回所有行WHERE region == 17的查询 (这是我的第一组行) 并且想要添加region为null的所有行 (这是我的第二组行) 但  如果两个集合中都有相同product_type和product_size的行,我想在最终结果中只是第一个集合的行。

示例:

pkey | region | product_type | product_size | price | desc

 1,    null,    20,            7,             2.70,   salad1    
 2,    null,    20,            3,             2.50,   salad7    
 3,    17,      20,            7,             1.90,   saladspecial    
 4,    17,      20,            5,             2.20,   other

我想要一个返回此命令的linq查询:

 2,    null,    20,            3,             2.50,   salad7    
 3,    17,      20,            7,             1.90,   saladspecial    
 4,    17,      20,            5,             2.20,   other

(请注意,丢弃带有pkey 1的行,因为pkey 3的行具有相同的product_type和product_size)

var query1 = from p in PRICES where p.region == 17    
             select p;

var query2 = from p in PRICES where p.region is null     
             select p;

问题:

  1. 如何连接query1和query2以获得预期的输出?

  2. 只需1次查询即可完成?

2 个答案:

答案 0 :(得分:2)

以下查询仅选择区域为17null的价格,请按唯一键{ p.product_type, p.product_size }对其进行分组。然后它检查组是否包含至少一个区域为17的价格。如果是,那么我们从组中选择该区域的所有价格(并跳过null区域的价格)。否则我们返回整个组(它只有空区域):

var query = from p in PRICES.Where(x => x.region == 17 || x.region == null)
            group p by new { p.product_type, p.product_size } into g
            from pp in g.Any(x => x.region == 17) ? 
                       g.Where(x => x.region == 17) : g
            select pp;

输入:

1 null 20 7 2.7 salad1       // goes to group {20,7} with region 17 price
2 null 20 3 2.5 salad7       // goes to group {20,3} without region 17 prices
3   17 20 7 1.9 saladspecial // goes to group {20,7}
4   17 20 5 2.2 other        // goes to group {20,5}

输出:

2 null 20 3 2.5 salad7 
3   17 20 7 1.9 saladspecial
4   17 20 5 2.2 other

编辑上面的查询适用于内存中的对象(即LINQ to Objects),但LINQ to Entitis不是那么强大 - 它不支持嵌套查询。因此,对于Entity Framework,您需要两个查询 - 一个用于获取null区域的价格,该区域在组中没有区域17价格,第二个 - 来自区域17的价格:

var pricesWithoutRegion = 
            db.PRICES.Where(p => p.region == 17 || p.region == null)
              .GroupBy(p => new { p.product_type, p.product_size })
              .Where(g => !g.Any(p => p.region == 17))
              .SelectMany(g => g);

var query = db.PRICES.Where(p => p.region == 17).Concat(pricesWithoutRegion);

实际上,EF在一个UNION查询服务器中执行两个子查询。将生成以下SQL(我删除了 desc price 列以适应屏幕):

SELECT [UnionAll1].[pkey] AS [C1], 
       [UnionAll1].[region] AS [C2], 
       [UnionAll1].[product_type] AS [C3], 
       [UnionAll1].[product_size] AS [C4]
FROM (SELECT [Extent1].[pkey] AS [pkey], 
             [Extent1].[region] AS [region], 
             [Extent1].[product_type] AS [product_type], 
             [Extent1].[product_size] AS [product_size]
      FROM [dbo].[Prices] AS [Extent1] WHERE 17 = [Extent1].[region]
UNION ALL
   SELECT [Extent4].[pkey] AS [pkey], 
          [Extent4].[region] AS [region], 
          [Extent4].[product_type] AS [product_type], 
          [Extent4].[product_size] AS [product_size]
   FROM (SELECT DISTINCT [Extent2].[product_type] AS [product_type], 
                         [Extent2].[product_size] AS [product_size]
         FROM [dbo].[Prices] AS [Extent2]
         WHERE ([Extent2].[region] = 17 OR [Extent2].[region] IS NULL) AND 
               (NOT EXISTS 
                (SELECT 1 AS [C1] FROM [dbo].[Prices] AS [Extent3]
                 WHERE ([Extent3].[region] = 17 OR [Extent3].[region] IS NULL)
                       AND ([Extent2].[product_type] = [Extent3].[product_type])
                       AND ([Extent2].[product_size] = [Extent3].[product_size])
                       AND (17 = [Extent3].[region])
                 ))) AS [Distinct1]
   INNER JOIN [dbo].[Prices] AS [Extent4] 
       ON ([Extent4].[region] = 17 OR [Extent4].[region] IS NULL)
          AND ([Distinct1].[product_type] = [Extent4].[product_type])
          AND ([Distinct1].[product_size] = [Extent4].[product_size]))
   AS [UnionAll1]

顺便说一下,我很惊讶GroupBy被翻译成带有条件的内连接。

答案 1 :(得分:1)

我认为你应该进行1次查询,对于2次查询,我们必须重复一些事情:

//for 2 queries
var query = query1.Union(query2.Except(query2.Where(x=>query1.Any(y=>x.product_type==y.product_type&&x.product_size==y.product_size))))
                  .OrderBy(x=>x.pkey);

//for 1 query
//the class/type to make the group key
public class GroupKey
{
        public int ProductType { get; set; }
        public int ProductSize { get; set; }
        public override bool Equals(object obj)
        {
            GroupKey gk = obj as GroupKey;
            return ProductType == gk.ProductType && ProductSize == gk.ProductSize;
        }
        public override int GetHashCode()
        {
            return ProductSize ^ ProductType;
        }
}
//-------
var query = list.Where(x => x.region == 17 || x.region == null)
                .GroupBy(x => new GroupKey{ProductType = x.product_type, ProductSize = x.product_size })
                .SelectMany<IGrouping<GroupKey,Price>,Price,Price>(x => x.Where(k => x.Count(y => y.region == 17) == 0 || k.region == 17), (x,g) => g)
                .OrderBy(x=>x.pkey);