我得到「LINQ to Entities不支持」LINQ表達式節點類型「Invoke」。這一點的代碼錯誤。將實體映射到poco獲取調用錯誤的方法
函數調用
IEnumerable<OrderListItem> orders;
orders = _service.GetAllForUser<OrderListItem>(userName, mapOrderToListItem);
var foo = orders.ToArray();
功能
public IEnumerable<T> GetAllForUser<T>(string userName, Func<Order, T> mapper)
{
string displayName = _adService.GetDisplayName(userName);
return _repo.Query().Where(x => x.OrderedBy == userName)
.OrderBy(x => x.OrderDate)
.Select(x => mapper(x))
.AsEnumerable();
}
private OrderListItem mapOrderToListItem(Order order)
{
OrderListItem result = new OrderListItem
{
DeliveryLoc = order.OrderGroup.DeliveryLocation.Name,
Department = order.OrderGroup.Department.Name,
Id = order.Id,
OrderDate = order.OrderDate,
OrderedBy = order.OrderedBy,
Status = !order.ApprovedDate.HasValue ? "SUBMITTED" : !order.ReceivedDate.HasValue ? order.ApprovalStatus == ApprovalStatus.Approved ? "APPROVED" : "DENIED" : !order.FilledDate.HasValue ? "RECEIVED" : "FILLED"
};
return result;
}
但是我不收到調用錯誤此位的代碼
函數調用
IEnumerable<ProductListItem> products;
products = _service.SearchAll<ProductListItem>(sSearch, 0, all, orderBy, sortDir, productListItemMapper);
var foo = products.ToArray();
功能
public IEnumerable<T> SearchAll<T>(string sSearch, int skip, int take, Func<Product, IComparable> sortCol, DAL.SortDir sortDir, Func<Product, T> mapper)
{
switch (sortDir)
{
case DAL.SortDir.asc:
return _repo.Query().Where(r => (r.Name.Contains(sSearch) || r.CatalogNumber.Contains(sSearch)))
.OrderBy(sortCol).Skip(skip).Take(take).Select(x => mapper(x)).AsEnumerable();
case DAL.SortDir.dsc:
return _repo.Query().Where(r => (r.Name.Contains(sSearch) || r.CatalogNumber.Contains(sSearch)))
.OrderByDescending(sortCol).Skip(skip).Take(take).Select(x => mapper(x)).AsEnumerable();
default:
throw new ArgumentException("sortDir");
}
}
private ProductListItem productListItemMapper(Product product)
{
ProductListItem output = new ProductListItem
{
Id = product.Id,
Location = product.WarehouseLocation.Name,
Name = product.Name,
Active = product.Active,
Number = product.CatalogNumber,
Units = product.UnitOfMeasure.Name,
UnitsId = product.UnitOfMeasureId,
LocationId = product.WarehouseLocationId
};
return output;
}
我覺得我只是盲目的和簡單的東西。爲什麼它適用於產品,但不適用於訂單?
使用表達式<等式>等 –
爲什麼Expression? –
Michael
EF只能將表達式轉換爲SQL,而不是函數。 –