Question

I will try to phrase this to the best of my abilities. I'm currently trying to grab a value called Program but I am having trouble with my LINQ query

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
    }));

The error I get is Object reference not set to an instance of an object.

When I comment out below it works but Program isn't brought through.

Program = x.Allocations.FirstOrDefault(y => y.Interaction_Id == x.Id).Program.Value,
Category = x.Allocations.FirstOrDefault().Category.Value

My goal from the LINQ query: Has to look up interactions, then get the Program/CategoryID from InterActionAllocations then gets the "value" from 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; }
Was it helpful?

Solution 2

I think the best way to tackle this is to store the Program and Category as intermediary result and then use the conditional operator (?). This works better with the let keyword in comprehensive syntax:

from i in _client.Interactions
let program = x.Allocations.Where(y => y.Interaction_Id == x.Id)
                           .Select(a => a.Program).FirstOrDefault()
let cat = x.Allocations.Select(a => a.Category).FirstOrDefault()
select new InteractionDTO
    {         
       Id = x.Id,
       ClientName = x.Person.CorrespondenceName,
       Indepth = x.Indepth,
       Program = program == null ? null : program.Value,
       Category = cat == null ? null : cat.Value,
       ActivityDate = x.ActivityDate,
       Type = x.Type,
       Subject = x.Subject,
       LoanApplicationProvided = x.LoanApplicationProvided,
       BusinessPlanProvided = x.BusinessPlanProvided
    }

OTHER TIPS

It would seem that at least one of your interactions doesn't have a corresponding program. You need the sub-query to support this case. One simple way is to do the transformation of program to program value before calling FirstOrDefault:

Program = x.Allocations.Where(y => y.Interaction_Id == x.Id)
    .Select(y => y.Program.Value)
    .FirstOrDefault(),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top