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 つのクエリに分割する必要がありますか?