使用 C# 4 和 EF5。
尝试对计算的属性求和时遇到问题:
The specified type member 'QtyAvailable' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
这是属性定义:
public decimal QtyAvailable
{
get
{ return this.QtyInContainer - this.QtyAllocated; }
}
这是Linq:
var viewModel =
companyItems
.GroupJoin(
inventory,
items => items.Id,
inven => inven.ItemId, (im, icd) => new { im, icd }
)
.SelectMany(
itemInven => itemInven.icd.DefaultIfEmpty(),
(im, icd) => new { im, icd }
)
.GroupBy(g => new
{
g.im.im.Id
}
)
.Select(group =>
new ItemAvailabilityViewModel()
{
Id = group.Key.Id,
QtyAvailable = group.Sum(jt => jt.icd == null ? 0 : jt.icd.QtyAvailable),
UnitCashPrice = group.Max(jt => jt.im.im.UnitSellPrice),
UnitCreditPrice = group.Max(jt => jt.im.im.UnitSellPrice * (1 + company.CashDiscountPct)),
CustomerUnitPrice = group.Max(jt => jt.im.im.UnitSellPrice * (1 + decCreditPriceMarkup))
}
)
;
不知道如何达到这个结果。我是否需要以某种方式将其分解为 2 个查询?