Linq to Entities Group总计算财产

时间:2013-02-26 15:39:23

标签: c#-4.0 linq-to-entities entity-framework-5

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

1 个答案:

答案 0 :(得分: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()
                        ;