I am muddling through the Commerce.MVC project (http://blog.wekeroad.com/mvc-storefront/mvc-storefront-preview-1-available/?disqus_reply=3672793#comment-3672793) which has a really nice LINQ pattern for repositories and services however LLBLGEN's LINQ provider doesn't like the "let" construct that is used. The "let" construct isn't needed however I would like to know why LINQ to Entities doesn't throw up but LINQ to LLBLGen does. Here is a code example from that project.
public IQueryable<Order> GetOrders() {
var orders = from o in _db.Orders
//*******
let items = GetOrderItems(o.OrderID)
let transactions = GetTransactions(o.OrderID)
//*******
select new Order
{
Status = (OrderStatus)o.OrderStatusID,
DateCreated = o.CreatedOn,
ID = o.OrderID,
OrderNumber = o.OrderNumber,
//*******
Items = new LazyList<OrderItem>(items),
Transactions = new LazyList<Transaction>(transactions),
//*******
ShippingAddress = GetAddresses().Where(x => x.ID == o.ShippingAddressID).SingleOrDefault(),
BillingAddress = GetAddresses().Where(x => x.ID == o.BillingAddressID).SingleOrDefault(),
ShippingMethod =GetOrderShippingMethod(o.ShippingMethod, o),
UserName = o.UserName,
UserLanguageCode=o.UserLanguageCode,
DateShipped=o.DateShipped,
EstimatedDelivery = o.EstimatedDelivery,
TrackingNumber=o.TrackingNumber,
TaxAmount=o.TaxAmount,
DiscountReason=o.DiscountReason,
DiscountAmount=o.DiscountAmount
};
return orders;
}
IQueryable<OrderItem> GetOrderItems(Guid orderID)
{
return from oi in GetOrderItems()
where oi.OrderID == orderID
select oi;
}
public IQueryable<OrderItem> GetOrderItems() {
SqlCatalogRepository catalog = new SqlCatalogRepository(_db);
return from oi in _db.OrderItems
select new OrderItem
{
OrderID = oi.OrderID,
Quantity = oi.Quantity,
DateAdded=oi.DateAdded,
LineItemPrice=oi.LineItemPrice,
Product = (from p in catalog.GetProducts()
where p.ID == oi.ProductID
select p
).SingleOrDefault(),
};
}
I left out the implementation for the LazyList but all it does is take a IQueryable<> and convert it to an IList when it is accessed. Now the "let" statements aren't needed because you could just call "Items = new LazyList<OrderItem>(GetOrderItems(o.OrderID))", and that works with the LINQ to LLBLGen provider however when the let statments are used the query to SQL is completely messed up. Just commenting the let statement out everything works fine again. Whats going on here?