在linq C#中不显示数据

eh57zj3b  于 2022-12-06  发布在  C#
关注(0)|答案(1)|浏览(171)

我有计算商品的Linq,问题是我传递的名称,它们不起作用ProductName,CompanyName,CustomerName,也许Linq中有错误?它产生了许多具有这些字段的匿名方法,但在ToList()之后一切都不起作用

public async Task<IEnumerable<SalesReportItem>> GetReportData(DateTime dateStart, DateTime dateEnd)
            {
                dateStart = new DateTime(2000, 1, 1);
                var context = await _contextFactory.CreateDbContextAsync();
                var queryable = context.SalesTransactionRecords.Join(
                        context.Products,
                        salesTransactionRecords => salesTransactionRecords.ProductId,
                        products => products.Id,
                        (salesTransactionRecords, products) =>
                            new
                            {
                                salesTransactionRecords,
                                products
                            })
                    .Join(context.Companies,
                        combinedEntry => combinedEntry.salesTransactionRecords.CompanyId,
                        company => company.Id,
                        (combinedEntry, company) => new
                        {
                            combinedEntry,
                            company
                        })
                    .Join(context.VendorCustomers,
                        combinedEntryAgain => combinedEntryAgain.combinedEntry.salesTransactionRecords.CustomerId,
                        vendorCustomer => vendorCustomer.Id,
                        (combinedEntryAgain, vendorCustomer) => new
                        {
                            CompanyName = combinedEntryAgain.company.Name,
                            CustomerName = vendorCustomer.Name,
                            ProductId = combinedEntryAgain.combinedEntry.products.Id,
                            ProductName = combinedEntryAgain.combinedEntry.products.Name,
                            combinedEntryAgain.combinedEntry.salesTransactionRecords.MovementType,
                            combinedEntryAgain.combinedEntry.salesTransactionRecords.Period,
                            combinedEntryAgain.combinedEntry.salesTransactionRecords.Quantity,
                            combinedEntryAgain.combinedEntry.salesTransactionRecords.Amount,
                        }).Where(x => x.Period >= dateStart && x.Period <= dateEnd)
                    .GroupBy(combinedEntryAgain => new
                        {
                            combinedEntryAgain.ProductId,
                            combinedEntryAgain.ProductName,
                            combinedEntryAgain.CompanyName,
                            combinedEntryAgain.CustomerName,
                        }
                    ).Select(x => new SalesReportItem
                    {
                        ProductId = x.Key.ProductId,
                        Quantity = x.Sum(a => a.Quantity),
                        Amount = x.Sum(x => (x.MovementType == TableMovementType.Income ? x.Amount : -(x.Amount)))
                    });
    
    
             var items = await queryable.ToListAsync();
    
                return _mapper.Map<IEnumerable<SalesReportItem>>(items);
            }
3zwjbxry

3zwjbxry1#

我的错误是我没有在select中指定字段,否则一切都是嗡嗡作响的,上面的代码是工作的

Select(x => new SalesReportItem
                    {
                        ProductId = x.Key.ProductId,
                        ProductName = x.Key.ProductName,
                        CompanyName = x.Key.CompanyName,
                        CustomerName = x.Key.CustomerName,
                        Quantity = x.Sum(x => (x.MovementType == TableMovementType.Income ? x.Quantity : - x.Quantity)),
                        Amount = x.Sum(x => (x.MovementType == TableMovementType.Income? x.Amount: - x.Amount))
                    });

谢谢你的帮助Hans Ke ing

相关问题