[英]LINQ error- cannot covert IEnumerable<IEnumerable<object>> to IEnumerable<object>
This is the code: 这是代码:
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);
Which is causing this error: 导致此错误的原因:
Cannot convert IEnumerable<IEnumerable<object>> to IEnumerable<object> 无法将IEnumerable <IEnumerable <object >>转换为IEnumerable <object>
I don't understand. 我不明白 What is the solution? 解决办法是什么?
It looks like you are selecting a collection of collections. 您似乎正在选择一个收藏集。 What you need is to flatten whatever you selecting. 您需要的是拼合您的选择。 You will achieve your goal by replacing the Select
with SelectMany
. 通过将Select
替换为SelectMany
可以实现您的目标。
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));
This is working exactly as I was looking for. 这完全符合我的要求。
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.