LINQ错误 - 无法转换IEnumerable <ienumerable <object>&gt;到IEnumerable <object> </object> </ienumerable <object>

时间:2015-02-05 02:18:21

标签: c# linq

这是代码:

IEnumerable<StockVendorHistoryModal> vendorhistoryList
    = Session.Query<PurchaseOrderLineItem>()
             .Where(popredicate)
             .Where(p => p.Stock.Number != null)
             .Select(c => new StockVendorHistoryModal
                    {
                        StockId = c.Stock.Id,
                        LastPurchaseDate = c.PurchaseOrder.OrderDate.ToString("yyyy/MM/dd"),
                        VendorNumber = c.PurchaseOrder.PurchaseOrderVendor.Vendor.Number,
                        LastPurchasePrice = c.UnitPrice,
                        LastTransactionDate =
                            c.LastTransactionDate != null ? c.LastTransactionDate.Value.ToString("yyyy/MM/dd") : null,
                        LeadTimeDays = GetDays(c.PurchaseOrder.OrderDate, c.LastTransactionDate),
                    })
             .GroupBy(d => d.VendorNumber).ToList()
             .Select(gpo => gpo.OrderByDescending(x => x.LastPurchaseDate)
             .Take(4); 

导致此错误:

  

无法转换IEnumerable&lt; IEnumerable&lt; object&gt;&gt;到IEnumerable&lt; object&gt;

我不明白。解决方案是什么?

3 个答案:

答案 0 :(得分:2)

看起来您正在选择一系列馆藏。你需要的是压扁你选择的任何东西。您可以将Select替换为SelectMany来实现目标。

答案 1 :(得分:0)

        IEnumerable<StockVendorHistoryModal> vendorhistoryList = Session.Query<PurchaseOrderLineItem>()
            .Where(popredicate)
            .Where(p => p.Stock.Number != null)
            .Select(c => new StockVendorHistoryModal
            {
                StockId = c.Stock.Id,
                LastPurchaseDate = c.PurchaseOrder == null ? null : c.PurchaseOrder.OrderDate.ToString("yyyy/MM/dd"),
                VendorNumber =   c.PurchaseOrder == null ? string.Empty : c.PurchaseOrder.PurchaseOrderVendor.Vendor.Number,
                VendorName = c.PurchaseOrder == null ? string.Empty : c.PurchaseOrder.PurchaseOrderVendor.Vendor.Name,
                LastPurchasePrice = c.UnitPrice,
                LastTransactionDate = c.LastTransactionDate != null ? c.LastTransactionDate.Value.ToString("yyyy/MM/dd") : null,
                LeadTimeDays = GetDays(c.PurchaseOrder.OrderDate, c.LastTransactionDate),
            }).GroupBy(d => d.VendorNumber)
            .SelectMany(gpo => gpo.OrderByDescending(x => x.LastPurchaseDate).Take(4)); 

这与我正在寻找的完全一样。

答案 2 :(得分:-1)

IEnumerable<StockVendorHistoryModal> vendorhistoryList
= Session.Query<PurchaseOrderLineItem>()
         .Where(popredicate)
         .Where(p => p.Stock.Number != null)
         .Select(c => new StockVendorHistoryModal
                {
                    StockId = c.Stock.Id,
                    LastPurchaseDate = c.PurchaseOrder.OrderDate.ToString("yyyy/MM/dd"),
                    VendorNumber = c.PurchaseOrder.PurchaseOrderVendor.Vendor.Number,
                    LastPurchasePrice = c.UnitPrice,
                    LastTransactionDate =
                        c.LastTransactionDate != null ? c.LastTransactionDate.Value.ToString("yyyy/MM/dd") : null,
                    LeadTimeDays = GetDays(c.PurchaseOrder.OrderDate, c.LastTransactionDate),
                })
         .GroupBy(d => d.VendorNumber).ToList()
         .Select(gpo => gpo.OrderByDescending(x => x.LastPurchaseDate)
         .Take(4)
         .Select(g => g.First());