1

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

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

唯一键是:product_type + product_size

我想做一个返回所有行 WHERE region == 17 的查询(这是我的第一组行)并且想要添加所有 region 为空的行(这是我的第二组行)但是如果有行两组中的 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 个查询即可完成?

4

2 回答 2

2

以下查询仅选择带有区域的价格17null按唯一键对它们进行分组{ p.product_type, p.product_size }。然后它检查 group 是否包含至少一个 price 和 region 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 没有那么强大 - 它不支持嵌套查询。因此,对于实体框架,您将需要两个查询 - 一个用于获取包含区域的价格,该区域在组null中没有区域17价格​​,第二个是来自 region 的价格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);

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

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被转化为带有条件的内部连接。

于 2013-08-01T21:42:40.007 回答
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);
于 2013-08-02T00:02:49.000 回答