Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Created November 13, 2014 00:15
Show Gist options
  • Save BryanWilhite/5ea9f1b65e2447becff5 to your computer and use it in GitHub Desktop.
Save BryanWilhite/5ea9f1b65e2447becff5 to your computer and use it in GitHub Desktop.
C#, LINQ: IQueryable with Tuple Extension Method
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&ltOrderHeader,OrderStatus&gt;
/// ii) An attempt was made to join with Tuple&ltOrderHeader,OrderStatus&gt;
/// </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