I have the below LINQ query:
dbContext.NAVSummaries
.Join(dbContext.NAVSummaries.DefaultIfEmpty(),
current => new
{
current.Portfolio,
PD = SqlFunctions.DatePart("dw", current.ValueDate) == 2 ? DbFunctions.AddDays(current.ValueDate, -3).Value :
SqlFunctions.DatePart("dw", current.ValueDate) == 1 ? DbFunctions.AddDays(current.ValueDate, -2).Value :
DbFunctions.AddDays(current.ValueDate, -1).Value
},
previous => previous == null ? null : new { previous.Portfolio, PD = previous.ValueDate },
(outer, inner) => new { outer, inner }
)
.Where(n => !dateStart.HasValue || n.outer.ValueDate.CompareTo(dateStart.Value) >= 0)
.Where(n => !dateEnd.HasValue || n.outer.ValueDate.CompareTo(dateEnd.Value) <= 0)
There are x number of records for a given ValueDate, and x-1 number of records on it's previous business date. The NAVSummaries DbSet is self-joined on the ValueDate column with the ValueDate item matching its previous business date. However, the output results a count of only x-1. I wish to perform a left-outer join so that all the x records of the left table/collection are returned.
EDIT: There will be only one record/item matching to the previous business date. So it has to be kind of One-To-One mapping.
NavSummary Entity :
public class NAVSummary
{
[Key, Column(Order = 0)]
public string Portfolio { get; set; }
[Key, Column(Order = 2)]
public DateTime ValueDate { get; set; }
public decimal BackOfficeNAV { get; set; }
public decimal FrontOfficeNAV { get; set; }
public decimal DifferencePercent { get; set; }
public decimal Threshold { get; set; }
public int ExtractId { get; set; }
public string ExtractStatus { get; set; }
public string PortfolioOwner { get; set; }
public DateTime DateTimeModified { get; set; }
public int MostCorrectNAV { get; set; }
public virtual IList<NAVComment> Comments { get; set; }
public virtual IList<NAVStatus> Statuses { get; set; }
public virtual IList<NAVExtract> Extracts { get; set; }
[JsonIgnore]
[NotMapped]
public bool IsChange { get; set; }
[NotMapped]
public decimal DayOverDayChange { get; set; }
[JsonIgnore]
[NotMapped]
public DateTime PreviousValueDate { get; set; }
[JsonIgnore]
[NotMapped]
public decimal PreviousDP { get; set; }
}