有条件地加入LINQ

时间:2013-01-21 17:36:44

标签: c# linq

我想创建一个等同于Left Join

的LINQ连接语句

我的表格设置如下:

Recipe
    RecipeID
    ...

Instruction
    RecipeID
    StepID
    SomeFlag
    ...

等效SQL:

SELECT *
FROM Recipe r
LEFT JOIN Instruction i
    ON r.RecipeID = i.RecipeID
    AND SomeFlag > 0

这是我到目前为止所做的:

var tmp = db.Recipe
    .GroupJoin(
        db.Instruction,
        r => r.RecipeID,
        i => i.RecipeID,
        (r, i) => new {r, i},
        ???);

首先,GroupJoin是这种操作的正确选择吗?据我所知,Join相当于SQL'Internal Join',而GroupJoin相当于'Left Join'。第二,获得我想要的结果的正确语法是什么?我一直在寻找一段时间,我似乎找不到合适的答案使用扩展方法

3 个答案:

答案 0 :(得分:5)

不要忘记阅读(GroupJoin:MSDN http://msdn.microsoft.com/en-us/library/bb535047.aspxJoin MSDN http://msdn.microsoft.com/fr-fr/library/bb534675.aspx

的帮助

GroupJoinJoin的最后一个参数是可选的(通过重载),通常不使用。 该功能允许您指定如何将r.RecipeIDi.RecipeID进行比较。由于RecipeID必须是整数,因此使用默认比较器是一个不错的选择。所以请用:

var tmp = db.Recipe
    .Join(db.Instruction,
          r => r.RecipeID,
          i => i.RecipeID,
          (r, i) => new {r, i});

现在您要删除具有SomeFlag > 0的所有说明。加入前为什么不这样做呢? 像这样:

var tmp = db.Recipe
    .Join(db.Instruction.Where(instruction => instruction.SomeFlag > 0),
          r => r.RecipeID,
          i => i.RecipeID,
          (r, i) => new {r, i});

更新

@usr已经完美评论说Join执行INNER JOIN。

正如您可能已经注意到的那样,LINQ没有针对INNER,OUTER,LEFT,RIGHT联接的不同方法。要了解特定SQL连接的等效LINQ,您可以在MSDN(http://msdn.microsoft.com/en-us/library/vstudio/bb397676.aspx)上找到帮助。

var tmp = from recipe in Recipes
          join instruction in
              from instruction in Instructions
              where instruction.SomeFlag > 0
              select instruction
          on recipe.RecipeID equals instruction.RecipeID into gj
          from instruction in gj.DefaultIfEmpty()
          select new
          {
              recipe,
              instruction
          };

使用扩展方法这是一个丑陋的解决方案:

var tmp = Recipes.GroupJoin(Instructions.Where(instruction => instruction.SomeFlag > 0),
                            recipe => recipe.RecipeID,
                            instruction => instruction.RecipeID,
                            (recipe, gj) => new { recipe, gj })
                 .SelectMany(@t => @t.gj.DefaultIfEmpty(), 
                             (@t, instruction) => new
                             {
                                 @t.recipe,
                                 instruction
                             });

答案 1 :(得分:1)

请告诉我,如果我不理解你,但这个扩展方法返回的结果与你在sql中提供的结果相同。

public static IEnumerable<ResultType> GetLeftJoinWith(this IEnumerable<Recipe>, IEnumerable<Instructions> ins)
{
    var filteredInstructions = ins.Where(x => x.SomeFlag > 0);

    var res = from r in rec
              join tmpIns in filteredInstructions on r.RecipeID equals t.RecipeID into instructions
              from instruction in instructions.DefaultIfEmpty()
              select new { r, instruction };

   return res;
}

答案 2 :(得分:0)

尝试

var model = db.Recipe
            .GroupJoin(db.Instructions.Where(instruction => instruction.SomeFlag > 0),r => r.RecipeID,i => i.RecipeID, (r, i) => new { Recipe = r, Instructions = i })
            .SelectMany(t => t.Instructions.DefaultIfEmpty(),(t, Instructions) => new
            {
                 Recipe = t.Recipe,
                 Instructions = Instructions
            });