简体   繁体   English

如何在实体框架中结合内连接和左连接

[英]How to combine inner join and left join in Entity Framework

I am using DBContext API from EF 4.1.我正在使用 EF 4.1 中的DBContext API。 Consider following entity model (A, B, E, D are entities)考虑以下实体 model(A、B、E、D 是实体)

A: aId答:艾德

B: aId, cId B: aId, cId

E: eId, aId E: eId, aId

D: eId, cId, Data D:eId、cId、数据

What I want is equivalent of below sql query我想要的相当于下面的 sql 查询

SELECT
   B.aId,
   B.cId,
   COALESCE(M.Data, [default value])
FROM
   B LEFT OUTER JOIN
   (
      SELECT
         E.aId,
         D.cId,
         D.Data  
      FROM
         E INNER JOIN D ON E.eId = D.eId
   ) M
   ON B.aId = M.aId AND B.cId = M.cId

Its simple to have left join on B, E & D but I found that I cannot solve above query.在 B、E 和 D 上留下连接很简单,但我发现我无法解决上述查询。 I have tried linq form of what I think would be an equivalent query我尝试过 linq 形式的我认为是等效查询的形式

// inner join equivalent
var ee = db.E.Join(db.D, e => e.eId, d => d.eId,
    (e, d) => new { e.aId, e.eId, d.cId, d.Data });

// left outer join
var o = from c in db.B
        join e in ee on new { c.aId, c.cId }
            equals new { e.aId, e.cId } into temp
        from m in temp.DefaultIfEmpty()
        select new
        {
            c.aId,
            c.cId,
            Data = null != m ? m.Data : [default value]
        };

However, this fails when I call o.ToString() with following exception details:但是,当我使用以下异常详细信息调用o.ToString()时,这将失败:

System.ArgumentException: The argument to DbIsNullExpression must refer to a primitive or reference type. System.ArgumentException:DbIsNullExpression 的参数必须引用原始类型或引用类型。 at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateIsNull(DbExpression argument, Boolean allowRowType) at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter parent, Expression input) at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)在 System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter 父级,表达式输入)在 System.Data.Objects. .ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

... [more stack trace out here] ... [更多堆栈跟踪在这里]

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.Convert()在 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.Convert()
at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery.ToTraceString() at System.Data.Entity.Internal.Linq.InternalQuery 1.ToString() at System.Data.Entity.Infrastructure.DbQuery`1.ToString()在 System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery.ToTraceString() at System.Data.Entity.Internal.Linq.InternalQuery 1.ToString() 在 System.Data .Entity.Infrastructure.DbQuery`1.ToString()

I have tried to form similar query using extension methods but had the same exception.我曾尝试使用扩展方法形成类似的查询,但有同样的例外。 What am I missing here?我在这里想念什么?

--------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------

EDIT:编辑:

It appears that issue was due to line看来问题是由于线路

Data = null != m ? m.Data : [default value]

I have modified it to我已将其修改为

   Data = m

And it started working.它开始工作了。 I have to move null checking logic at the place where I am using the result.我必须在我使用结果的地方移动 null 检查逻辑。 Now, I am wondering what can be the cause of exception?现在,我想知道异常的原因是什么? From exception details, it appears that it cannot figure out m (which is an anonymous type) as reference type.从异常细节来看,它似乎无法将 m (它是一个匿名类型)确定为引用类型。 Is this behavior documented somewhere?这种行为是否记录在某处?

Well you were null checking a joined entity, which doesn't make sense in terms of sql.好吧,您是 null 检查连接实体,这对于 sql 而言没有意义。 The following should be interpreted properly as a coalesce:以下内容应正确解释为合并:

Data = m.Data ?? [default value]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM