C# 左连接linq不工作

C# 左连接linq不工作,c#,linq,linq-to-sql,linq-to-entities,C#,Linq,Linq To Sql,Linq To Entities,我有一个linq查询 var x = (from d in detalle from n in nlj.DefaultIfEmpty() join nd in nuevodetalle on new { d.ope_idsku ,d.ope_tipo } equals new {nd.ope_idsku ,nd.ope_tipo }into nlj from n in nlj.DefaultIfEmpty() select new { d, n } ).ToList(); 现在如果我这么做

我有一个linq查询

var x = (from d in detalle
from n in nlj.DefaultIfEmpty()
join nd in nuevodetalle
on new { d.ope_idsku ,d.ope_tipo } equals new {nd.ope_idsku ,nd.ope_tipo }into nlj
from n in nlj.DefaultIfEmpty()


select new { d, n } ).ToList();
现在如果我这么做了

x[0].d==null this return false
x[0].n==null this return false
x[1].d==null this return false
x[1].n==null this fails, why? 
我真正的选择不是{d,n}它是

select new ope_detalle_autoventa()
{
CreatedOn = d.CreatedOn,
ModifiedOn = d.ModifiedOn,
ope_autoventaid = d.ope_autoventaid,
ope_Codope = d.ope_Codope,
ope_detalle_autoventaId = (tiene_nuevo_primarykey) ? Guid.NewGuid() : d.ope_detalle_autoventaId,
ope_entregado = (d.ope_entregado - (n != null ? n.ope_entregado : 0)),
ope_envase = d.ope_envase,
ope_estado = d.ope_estado,
ope_icono = d.ope_icono,
ope_idsku = d.ope_idsku,
ope_name = d.ope_name,
ope_pedido = d.ope_pedido,
ope_precioV = d.ope_precioV,
ope_prestamo = d.ope_prestamo,
ope_promocion = d.ope_promocion,
ope_skuid = d.ope_skuid,
ope_tipo = d.ope_tipo,
ope_autoventaidTarget = d.ope_autoventaidTarget,
ope_skuidTarget = d.ope_skuidTarget,
Ope_NumVenta = d.Ope_NumVenta,
ope_descuento = d.ope_descuento,
ope_cancelado = d.ope_cancelado,
ope_folio = d.ope_folio,
ope_cantcanc = 0,
ope_estimado = "0"
}
但是这个get失败的原因与[code>x[1]相同。n==null失败(我相信), 我认为这在这方面是失败的

ope_entregado = (d.ope_entregado - (n != null ? n.ope_entregado : 0)),
然而 这个查询没有失败

var restaragregados = (from r in resultadoregresar
group new { r.ope_entregado } by new { r.ope_idsku } into agrupacion
select new { agrupacion.Key.ope_idsku, entregadototal=(agrupacion.Sum (x=> x.ope_entregado) ) }
);
List<ope_detalle_autoventa> nuevodetalle = (from d in detalle
join c in cargainventario
on d.ope_idsku equals c.ope_idsku into clj
from c in clj.DefaultIfEmpty()
join r in restaragregados on
d.ope_idsku equals r.ope_idsku into rlj
from r in rlj.DefaultIfEmpty ()
where (c!=null?c.ope_carga == cargausada:c==null  )
&& (d.ope_entregado > ((c==null ?0:c.ope_disponibles ) - (r==null?0:r.entregadototal)))
&& d.ope_tipo ==tipos[i]
select new ope_detalle_autoventa()
{
ModifiedOn = DateTime.Now,
ope_autoventaid = ope_autoventaid,
ope_detalle_autoventaId = d.ope_detalle_autoventaId,
ope_entregado = (d.ope_entregado - ((c== null ? 0 : c.ope_disponibles) - (r == null ? 0 : r.entregadototal))),
ope_envase = d.ope_envase,
ope_estado = d.ope_estado,
ope_icono = d.ope_icono,
ope_idsku = d.ope_idsku,
ope_name = d.ope_name,
ope_pedido = 0,//0 por que no pidio de esta carga sino de la que ya se gastó
ope_precioV = d.ope_precioV,
ope_prestamo = d.ope_prestamo,
ope_promocion = d.ope_promocion,
ope_skuid = d.ope_skuid,
ope_tipo = d.ope_tipo,
ope_autoventaidTarget = d.ope_autoventaidTarget,
ope_skuidTarget = d.ope_skuidTarget,
Ope_NumVenta = d.Ope_NumVenta,
ope_descuento = d.ope_descuento,
ope_cancelado =d.ope_cancelado ,
CreatedOn =DateTime.Now ,
ope_Codope =d.ope_Codope ,
ope_folio =d.ope_folio,
ope_cantcanc =0,
ope_estimado="0"
}
).ToList();
但我也犯了同样的错误

我解决了这个问题

 if (nuevodetalle.Count == 0)
                nuevodetalle.Add(detalle [0]);
            ope_detalle_autoventa clone = (ope_detalle_autoventa)detalle[0].Clone();
            clone.ope_idsku = 0;
            clone.ope_entregado = 0;
            clone.ope_tipo = 0;
            List<ope_detalle_autoventa> detallecargaactual = new List<ope_detalle_autoventa>();


            var x = (
                from d in detalle
                join c in nuevodetalle
                //on new { d.ope_idsku, d.ope_tipo } equals new { c.ope_idsku, c.ope_tipo } into clj
                on d.ope_idsku equals c.ope_idsku into clj
                from c in clj.DefaultIfEmpty (clone)
                select new{ d, c}).ToList();`enter code here
if(nuevodetalle.Count==0)
添加(detalle[0]);
ope_detalle_autoventa克隆=(ope_detalle_autoventa)detalle[0]。克隆();
clone.ope_idsku=0;
clone.ope_entregado=0;
clone.ope_tipo=0;
List detallecargaactual=新列表();
变量x=(
从德塔勒的d
在新德里加入c
//关于新的{d.ope_-idsku,d.ope_-tipo}等于clj中的新的{c.ope_-idsku,c.ope_-tipo}
关于d.ope_idsku等于c.ope_idsku到clj
从clj.DefaultIfEmpty(克隆)中的c
选择新的{d,c}).ToList()`在这里输入代码

您可以看到我已更改
clj.DefaultIfEmpty(克隆)

将代码向右移动一点。我仍然可以看到一些字符。谢谢-为了可读性,最好留下一些缩进,只是不足以水平滚动大部分代码。我认为问题在于两个匿名对象new{d.ope_idsku,d.ope_tipo}和new{nd.ope_idsku,nd.ope_tipo}将永远不会相等,因为默认的相等方法将比较引用而不是内容。我将尝试使用d.ope_idsku等于nd.ope_idsku,并使用where for add d.ope_tipo=nd.ope_tipo,我会说you@Casperah匿名对象的
Equals
方法被重写,用于对对象的所有属性进行成员式比较,而不是专门比较引用,以便您可以准确地执行此操作。无论如何,它使用的是一个查询提供程序,而不是linq to对象,因此它无论如何也不会调用
Equals
 if (nuevodetalle.Count == 0)
                nuevodetalle.Add(detalle [0]);
            ope_detalle_autoventa clone = (ope_detalle_autoventa)detalle[0].Clone();
            clone.ope_idsku = 0;
            clone.ope_entregado = 0;
            clone.ope_tipo = 0;
            List<ope_detalle_autoventa> detallecargaactual = new List<ope_detalle_autoventa>();


            var x = (
                from d in detalle
                join c in nuevodetalle
                //on new { d.ope_idsku, d.ope_tipo } equals new { c.ope_idsku, c.ope_tipo } into clj
                on d.ope_idsku equals c.ope_idsku into clj
                from c in clj.DefaultIfEmpty (clone)
                select new{ d, c}).ToList();`enter code here