我會嘗試將這個短語盡我所能。目前,我想要抓住一個叫Program
的價值,但我有我的LINQ查詢的麻煩LINQ查詢中的空引用
Interactions = new BindableCollection<InteractionDTO>(_client.Interactions.
Select(x => new InteractionDTO
{
Id = x.Id,
ClientName = x.Person.CorrespondenceName,
Indepth = x.Indepth,
Program = x.Allocations.FirstOrDefault(y => y.Interaction_Id == x.Id).Program.Value,
Category = x.Allocations.FirstOrDefault().Category.Value,
ActivityDate = x.ActivityDate,
Type = x.Type,
Subject = x.Subject,
LoanApplicationProvided = x.LoanApplicationProvided,
BusinessPlanProvided = x.BusinessPlanProvided
}));
我得到的錯誤是Object reference not set to an instance of an object.
當我註釋掉下面它的工作原理,但Program
而不使通過。
Program = x.Allocations.FirstOrDefault(y => y.Interaction_Id == x.Id).Program.Value,
Category = x.Allocations.FirstOrDefault().Category.Value
從LINQ查詢我的目標:有擡頭的交互門,然後從InterActionAllocations
的Program
/CategoryID
然後會從InteractionPrograms
「價值」。
的Program.cs
// Primary Keys -------------------------------------------------------
public int Id { get; set; }
[InverseProperty("Interaction")]
public virtual ICollection<InteractionAllocation> Allocations { get; set; }
InteractionAllocation.cs
// Associations -------------------------------------------------------
public int Interaction_Id { get; set; }
[ForeignKey("Interaction_Id")]
public virtual Interaction Interaction { get; set; }
public int? Category_Id { get; set; }
[ForeignKey("Category_Id")]
public InteractionCategory Category { get; set; }
您需要花費一些時間進行調試。要麼分配爲空,要麼沒有一個具有正確的interaction_id,要麼有空程序,要麼類別爲空。 –