可以为空的对象必须具有值。 Linq FK

时间:2015-01-01 06:24:10

标签: c# linq

我对linq很新,每次我加入时都会收到此错误。我加入的其中一个字段是可以为空的。

try
   {
    DCEDataContext dc = new DCEDataContext();
    List<Account> AcctWithChild=new List<Account>();
    AcctWithChild.Add(Account.GetById(3));
     //var query = (from acct in AcctWithChild
         List<CTRAC.WebApi.Models.Organization> Organizations = (from acct in AcctWithChild
                       join allOrg in dc.Organizations on acct.ID equals allOrg.AccountID
                       join allEngage in dc.Engagements on allOrg.in_id equals allEngage.OrganizationID
                       join allEngageForm in dc.EngagementForms on allEngage.ID equals allEngageForm.EngagementID
                       join allEnFormSub in dc.EngagementFormFilingSubmissions on allEngageForm.ID equals allEnFormSub.EngagementFormID
                       into EngagementFormFilingSubGroup
                       from allEnFormSub in EngagementFormFilingSubGroup.DefaultIfEmpty()
                       where allOrg.AccountID.HasValue
                       select new CTRAC.WebApi.Models.Organization { Name = allOrg.vc_BusinessName, OrganizationID = allOrg.OrganizationId,Return990_Id=allEngageForm.ID, EfileSubmissionDate=allEnFormSub.Timestamp }).Take(100).ToList();

编辑当我制作EfileSubmisionDate = null时,它会通过但将其设置为allEnFormSub.Times,即sumtimes null会给我一个错误。

1 个答案:

答案 0 :(得分:0)

可能正在使用HasValue

 List<CTRAC.WebApi.Models.Organization> Organizations = (from acct in AcctWithChild    
         join allOrg in dc.Organizations on acct.ID equals allOrg.AccountID??0
                       let MYID=allOrg.Field<Int?>("AccountID")
                       where MYID.HasValue
                       select new CTRAC.WebApi.Models.Organization { Name = allOrg.vc_BusinessName, OrganizationID = allOrg.OrganizationId,Return990_Id=5, EfileSubmissionDate=acct.TrialBegin.Value }).ToList();