Hinsura
2/2/2018 - 2:20 PM

Get - Join Lists

var q = from p in _productManager.GetAllPlan()
        join pc in _productManager.GetAllPlanCoverages()
            on p.Id equals pc.PlanId
        where p.ProductId.Equals(productId)
        select new { pc.CoverageId };
var planCoveragesList = q.ToList();

/* if any one from the ProductCoverage list exists in the PlanCoverage list - we can NOT delete anything */
if (planCoveragesList.Any(x => coverageIds.Contains(x.CoverageId)))
{
    throw new UserFriendlyException("Bu kayıtla ilgili plan teminatları olduğu için silinemez.");
}
/* Fetch Persons data and convert to a List for alter joining */
var q1 = _productManager.GetAllPlan();
if (!string.IsNullOrWhiteSpace(productId))
{
  q1 = q1.Where(r => r.ProductId.Equals(productId));
}
var planList = await q1.ToListAsync();

/* Fetch Booklets data and convert to a List for later joining */
var bookletsList = await _productManager.GetAllBookletList();

/* Join Lists as IENumerable */
var q2 = from p in planList
         join b in bookletsList
         on p.BookletId equals b.Id into bookletView
         from bV in bookletView.DefaultIfEmpty()/* left outer join */
         select new
         {
             p.IsApproved,
             p.Id,
             p.Code,
             p.RuleEnginePlanCode,
             p.ExternalCode,
             p.Name,
             DisplayName = p.ToString(),
             p.ProductId,
             p.Year,
             p.SagmerCode,
             BookletName = bV?.Name
         };

/* Search Filtering */
var searchVal = request.Search.Value;
if (!string.IsNullOrWhiteSpace(searchVal))
{
  searchVal = searchVal.ToUpper();
  q2 =
    q2.Where(
      r =>
        r.Name.ToUpper().Contains(searchVal) ||
        r.Code.ToUpper().Contains(searchVal)
    );
}

/* Sort Ordering */
q2 = q2.OrderBy(r => r.ExternalCode).ThenByDescending(r => r.Id);
/* to get nulls last; */
// q2.OrderBy(r => r.ExternalCode ?? "9999999999999999999999999").ThenByDescending(r => r.Id);

/* Paging */
var count = q2.Count();
var dataList = request.Length > -1 ? q2.Skip(request.Start).Take(request.Length) : q2;/* -1 means all - "Hepsi" */