1

使用 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 个查询?

4

1 回答 1

2

看起来像把它分成 2 个查询是票。从我猜发生的情况来看,首先,当您调用 .ToList() 时,它会解析计算的属性,使其本质上不再为第二个查询计算然后对其求和。这是生成的代码:

//join of Recpt Header and Detail
            var unGroupedJoin =
                companyItems
                        .GroupJoin(
                            inventory,
                            items => items.Id,
                            inven => inven.ItemId, (im, icd) => new { im, icd }
                            )
                        .SelectMany(
                            itemInven => itemInven.icd.DefaultIfEmpty(),
                            (im, icd) => new { im, icd }
                        ).ToList();

            var viewModel =
                unGroupedJoin
                        .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))
                                }
                        ).ToList()
                        ;
于 2013-02-26T15:49:07.147 回答