c# - LINQ 中的关系划分?

标签 c# sql linq relational-algebra relational-division

关系划分是Codd之一的原始关系运算符,俗称供应所有零件的供应商。已经有各种翻译成 SQL,例如Celko 使用 the pilots who can fly all the planes in the hangar 的例子讨论了几种方法.

我更喜欢的是“用集合运算符除法”,因为它是“带余数”(即 Wilson 也可以驾驶 F-17 战斗机,但机库中没有)以及它如何处理这种情况除数是空集(即当机库为空时,所有飞行员都会返回):

WITH PilotSkills
     AS
     (
      SELECT * 
        FROM (
              VALUES ( 'Celko', 'Piper Cub' ), 
                     ( 'Higgins', 'B-52 Bomber' ), ( 'Higgins', 'F-14 Fighter' ),
                     ( 'Higgins', 'Piper Cub' ), 
                     ( 'Jones', 'B-52 Bomber' ), ( 'Jones', 'F-14 Fighter' ),
                     ( 'Smith', 'B-1 Bomber' ), ( 'Smith', 'B-52 Bomber' ),
                     ( 'Smith', 'F-14 Fighter' ),
                     ( 'Wilson', 'B-1 Bomber' ), ( 'Wilson', 'B-52 Bomber' ),
                     ( 'Wilson', 'F-14 Fighter' ), ( 'Wilson', 'F-17 Fighter' )
             ) AS T ( pilot_name, plane_name )
     ), 
     Hangar
     AS
     (
      SELECT * 
        FROM (
              VALUES ( 'B-1 Bomber' ), 
                     ( 'B-52 Bomber' ), 
                     ( 'F-14 Fighter' )
             ) AS T ( plane_name )
     )
SELECT DISTINCT pilot_name 
  FROM PilotSkills AS P1
 WHERE NOT EXISTS (
                   SELECT plane_name 
                     FROM Hangar
                   EXCEPT
                   SELECT plane_name
                     FROM PilotSkills AS P2
                    WHERE P1.pilot_name = P2.pilot_name
                  );

现在我需要在 LINQ to Objects 中执行此操作。这是一个建议的直接翻译:

var hangar = new [] 
{ 
    new { PlaneName = "B-1 Bomber" },
    new { PlaneName = "F-14 Fighter" },
    new { PlaneName = "B-52 Bomber" }
}.AsEnumerable();

var pilotSkills = new [] 
{ 
    new { PilotName = "Celko", PlaneName = "Piper Cub" },
    new { PilotName = "Higgins", PlaneName = "B-52 Bomber" },
    new { PilotName = "Higgins", PlaneName = "F-14 Fighter" },
    new { PilotName = "Higgins", PlaneName = "Piper Cub" },
    new { PilotName = "Jones", PlaneName = "B-52 Bomber" },
    new { PilotName = "Jones", PlaneName = "F-14 Fighter" },
    new { PilotName = "Smith", PlaneName = "B-1 Bomber" },
    new { PilotName = "Smith", PlaneName = "B-52 Bomber" },
    new { PilotName = "Smith", PlaneName = "F-14 Fighter" },
    new { PilotName = "Wilson", PlaneName = "B-1 Bomber" },
    new { PilotName = "Wilson", PlaneName = "B-52 Bomber" },
    new { PilotName = "Wilson", PlaneName = "F-14 Fighter" },
    new { PilotName = "Wilson", PlaneName = "F-17 Fighter" }
}.AsEnumerable();

var actual = pilotSkills.Where
(
    p1 => hangar.Except
    ( 
        pilotSkills.Where( p2 => p2.PilotName == p1.PilotName )
                    .Select( p2 => new { p2.PlaneName } )
    ).Any() == false 
).Select( p1 => new { p1.PilotName } ).Distinct();

var expected = new [] 
{
    new { PilotName = "Smith" },
    new { PilotName = "Wilson" }
};

Assert.That( actual, Is.EquivalentTo( expected ) );

作为LINQ is supposedly based on the relational algebra那么直接翻译似乎是合理的。但是是否有更好的“本地”LINQ 方法?


考虑到@Daniel Hilgarth 的回答,在 .NET Land 中,数据很可能首先“分组”:

var pilotSkills = new [] 
{ 
    new { PilotName = "Celko", 
            Planes = new [] 
            { new { PlaneName = "Piper Cub" }, } },
    new { PilotName = "Higgins", 
            Planes = new [] 
            { new { PlaneName = "B-52 Bomber" }, 
              new { PlaneName = "F-14 Fighter" }, 
              new { PlaneName = "Piper Cub" }, } },
    new { PilotName = "Jones", 
            Planes = new [] 
            { new { PlaneName = "B-52 Bomber" }, 
              new { PlaneName = "F-14 Fighter" }, } },
    new { PilotName = "Smith", 
            Planes = new [] 
            { new { PlaneName = "B-1 Bomber" }, 
              new { PlaneName = "B-52 Bomber" }, 
              new { PlaneName = "F-14 Fighter" }, } },
    new { PilotName = "Wilson", 
            Planes = new [] 
            { new { PlaneName = "B-1 Bomber" }, 
              new { PlaneName = "B-52 Bomber" }, 
              new { PlaneName = "F-14 Fighter" }, 
              new { PlaneName = "F-17 Fighter" }, } },
};

...并且仅投影名称是任意的,使潜在的解决方案更加直接:

 // Easy to understand at a glance:
var actual1 = pilotSkills.Where( x => hangar.All( y => x.Planes.Contains(y) ));

// Potentially more efficient:
var actual = pilotSkills.Where( x => !hangar.Except( x.Planes ).Any() );

最佳答案

以下应该产生相同的结果:

pilotSkills.GroupBy(x => x.PilotName, x => x.PlaneName)
           .Where(g => hangar.All(y => g.Contains(y.PlaneName)))

这将为每个飞行员返回一组,可以驾驶机库中的所有飞机。
组的key是飞行员的名字,组的内容是飞行员可以驾驶的所有飞机,包括那些不在机库的飞机。

如果您现在只需要飞行员,您可以在查询末尾添加 .Select(g => new { PilotName = g.Key })


将上述方法与 Except 结合使用,使其更接近 OP 的原始内容:

pilotSkills.GroupBy(x => x.PilotName, x => new { x.PlaneName } )
           .Where(g => !hangar.Except(g).Any());

第二个查询可能更好,因为它只迭代一次 g;第一个包含 Contains 的查询将它迭代 N 次,其中 N 是机库中的飞机数量。

关于c# - LINQ 中的关系划分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16209651/

相关文章:

c# - C#7.0 中的局部函数与 foreach 或循环有何不同?

c# - 将 UI 定位到鼠标位置(使工具提示面板跟随光标)

c# - .NET 中的每个文件规则一类?

当包含执行计划时,SSMS 中的 SQL Server 查询速度更快

c# - 如何将此 LINQ 与多个 "Where"一起使用?

C# uwp 启动应用程序

sql - 无法在查询中从 TextBox 进行部分匹配

sql - 在 SQL 中使用 RANK() OVER 将 rank 设置为 NULL

c# - 使用 LINQ 填充列表

c# - Linq 选择加入