LINQ: implementing Left Outer Join

Standard

Most of you are aware of the fact that there is no defacto way available for implementing outer join in LINQ, yes its true no keyword is associated with this functionality, however we can achieve that by using .DefaultIfEmpty() method. (We can also use extension method for our ease and customization but always remember KISS principle [Keep it simple stupid]

[HttpGet]
public JsonResult GetUserSavedDataState()
{
    CandidateInfoModels _userInfo = CurrentUser.GetCurrentUser();
    var currentState =
        (from personal in _context.CandidateDetails
            join job in _context.CandidatePaymentOptions on personal.ImportedCandidate_OID equals job.ImportedCandidate_OID
                into personalPaymentGroup
            from outer1 in personalPaymentGroup.Where(p => p.Active_f).DefaultIfEmpty()
            select new
            {
                Personal = personal.IsConfirmed == null ? false : personal.IsConfirmed,
                Job = outer1.IsConfirmed_f ?? false
            });
    return Json(currentState.Any() ? currentState.FirstOrDefault() : null, JsonRequestBehavior.AllowGet);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s