Created
November 13, 2014 00:15
-
-
Save BryanWilhite/5ea9f1b65e2447becff5 to your computer and use it in GitHub Desktop.
C#, LINQ: IQueryable with Tuple Extension Method
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
using System; | |
using System.Linq; | |
using System.Linq.Expressions; | |
namespace Fox.Xavier.Models.Extensions | |
{ | |
using Fox.Entities; | |
using Fox.Xavier.Models; | |
/// <summary> | |
/// Extensions of <see cref="OrderHeader"/> | |
/// </summary> | |
public static class OrderHeaderExtensions | |
{ | |
/// <summary> | |
/// Converts the <see cref="OrderHeader" /> into a <see cref="Requisition"/>. | |
/// </summary> | |
/// <remarks> | |
/// This did not work with EF when: | |
/// i) An attempt was made to cast to Tuple<OrderHeader,OrderStatus> | |
/// ii) An attempt was made to join with Tuple<OrderHeader,OrderStatus> | |
/// </remarks> | |
public static IQueryable<Requisition> ToRequisition(this IQueryable<Tuple<OrderHeader,OrderStatus>> queryable) | |
{ | |
Expression<Func<Tuple<OrderHeader, OrderStatus>, Requisition>> storeExpression = | |
input => new Requisition | |
{ | |
OrderId = input.Item1.OrderId, | |
OrderNumber = input.Item1.OrderNumber, | |
PONumber = input.Item1.PONumber, | |
OrderStatusID = input.Item1.OrderStatusID, | |
DepartmentID = input.Item1.DepartmentID, | |
CustomerName = input.Item1.CustomerName, | |
CreatedDt = input.Item1.CreatedDt, | |
PriorityFl = input.Item1.PriorityFl, | |
OrderStatusDescription = input.Item2.StatusDescription, | |
CreatedBy = input.Item1.CreatedBy, | |
OrderTotal = input.Item1.OrderDetails.Sum(i => i.TotalAmount) | |
}; | |
return queryable.Select(storeExpression); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment