Created
November 13, 2014 00:18
-
-
Save BryanWilhite/219bf168c391a3642a6e to your computer and use it in GitHub Desktop.
C#, EF: How to get OUTER JOIN and EXISTS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Queries the requisitions. | |
/// </summary> | |
public static IQueryable<Requisition> QueryRequisitions() | |
{ | |
var ctx = GetContext(); | |
var statusIdsQuery = ctx.OrderStatuses | |
.Select(i => i.OrderStatusId); | |
var orderIdsQuery = ctx.OrderDetails | |
.Where(i => i.BusinessType == "R") | |
.Select(i => i.OrderID); | |
var rentalChildrenQuery = XavierContext.QueryRentalChildren() | |
.Select(i => i.OrderId); | |
var query = ctx.OrderHeaders | |
.Include("OrderDetails") | |
.Where(i => i.OrderId != null) | |
.Where(i => i.RequisitionType == "R") | |
.Where(i => statusIdsQuery.Contains(i.OrderStatusID)) | |
.Where(i => !orderIdsQuery.Contains(i.OrderId)) | |
.Where(i => i.IsRentalParent == false) | |
.Where(i => !rentalChildrenQuery.Contains(i.OrderId)) | |
.OrderByDescending(i => i.OrderId) | |
.Select(i => new Requisition | |
{ | |
OrderId = i.OrderId, | |
OrderNumber = i.OrderNumber, | |
PONumber = i.PONumber, | |
DepartmentName = ctx.Departments.FirstOrDefault(j => j.DepartmentID == i.DepartmentID).DepartmentName, | |
CustomerName = i.CustomerName, | |
CreatedDt = i.CreatedDt, | |
PriorityFl = i.PriorityFl, | |
OrderStatusDescription = ctx.OrderStatuses.FirstOrDefault(j => j.OrderStatusId == i.OrderStatusID).StatusDescription, | |
CreatedBy = i.CreatedBy, | |
OrderTotal = i.OrderDetails.Sum(j => j.TotalAmount) | |
}); | |
return query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment