C# 为什么这在EF中不起作用

C# 为什么这在EF中不起作用,c#,entity-framework,exception,error-handling,C#,Entity Framework,Exception,Error Handling,有人能解释一下为什么这不起作用吗 private void AdditionalLoad(List<PersonTypePerson> ptps) { using(var _context = new SPIS_Entities()) { var m = (from ptp in ptps.Where(xx => xx.PersonID == _Person.ID)//_context.PersonTypePers

有人能解释一下为什么这不起作用吗

private void AdditionalLoad(List<PersonTypePerson> ptps)
    {
        using(var _context = new SPIS_Entities())
        {
            var m = (from ptp in ptps.Where(xx => xx.PersonID == _Person.ID)//_context.PersonTypePerson //
                     join pt in _context.PersonType on ptp.PersonTypeID equals pt.ID
                     join pta in _context.PersonTypeAttriubute.Where(p => p.active) on pt.ID equals pta.PersontypeID
                     select new { persontypeatribute = pta, availableatribute = pta.Attribute }).ToList();


            var x = from ii in _context.InformationItem.Where(p=>p.PersonID ==_Person.ID)
                    join pta in _context.PersonTypeAttriubute.Where(p => p.active) on ii.PersonTypeAtributeID equals pta.ID
                    select new { persontype = pta.PersonType, attribute = pta.Attribute, information = ii };


            var z = (from all in m
                     join fill in x on all.persontypeatribute.AttributeID equals fill.attribute.ID into ps
                     from fill in ps.DefaultIfEmpty()
                     select new
                     {
                         persontype = all.persontypeatribute.PersonType, //fill.persontype,
                         available = all.availableatribute,
                         attribute = fill.attribute,
                         information = fill.information
                     }).ToList();

        }
    }
private void AdditionalLoad(列出PTP)
{
使用(var _context=new SPIS_Entities())
{
var m=(来自ptps.Where(xx=>xx.PersonID==\u Person.ID)/\u context.PersonTypePerson中的ptp//
在ptp上的_context.PersonType中加入pt.PersonTypeID等于pt.ID
在_context.PersonTypeAttriubute中加入pta,其中pt.ID上的(p=>p.active)等于pta.PersontypeID
选择新的{persontypeatribute=pta,availableAttribute=pta.Attribute}).ToList();
var x=来自_context.InformationItem.Where中的ii(p=>p.PersonID==\u Person.ID)
在_context.PersonTypeAttriubute中加入pta,其中(p=>p.active)在ii.personTypeAttriubuteId等于pta.ID
选择新建{persontype=pta.persontype,attribute=pta.attribute,information=ii};
var z=(从m中的所有
将all.personTypeAttributeId等于fill.attribute.ID的所有.personTypeAttributeBute.AttributeID上的填充x连接到ps中
从填写ps.DefaultIfEmpty()开始
选择新的
{
persontype=all.persontypeatribute.persontype,//fill.persontype,
available=all.AvailableAttribute,
attribute=fill.attribute,
信息=fill.information
}).ToList();
}
}

最有趣的是,我从“m”和“x”得到一个查询结果。当要进行左连接(“z”)时,它会中断并抛出nullException,对象引用未设置为对象的实例。

堆栈跟踪可能会有所帮助。在我看来,您正试图将内存中的列表(
m
)与数据库查询(
x
)连接起来,这可能会导致
x
在最后一个查询中首先执行,最后一个查询中的连接是内存中的连接。如果
DefaultIfEmpty()
返回默认为匿名对象的集合,则可能会崩溃。默认值为
null
,您在最后一次
选择中使用
fill.attribute
fill.information
访问它。但这只是一个猜测…我想,像这样的事情可能是个问题