c# - 如何缩短 linq 查询?

标签 c# linq

我必须在程序中为每一份账单显示一行,但一份账单有多个子账单。我想将 2 个表中的信息分组到一个 DataGridView 中。 1 行必须显示有关所有子帐单的分组信息。 只有两列比其他列更复杂:“描述”和“总和”。 “描述”必须包含子账单的所有描述,并以换行符分隔。我用下面的方法做到了:

var documentQuery = (from doc in entities.Bas_DocumentData
                select new 
                        {
                            DocumentNum = doc.DocumentNum,
                            DocumentDate = doc.DocumentDate,
                            Description = doc.Bas_BonusData.Select(t => t.Description),
                            BillsCount = doc.Bas_BonusData.Count,
                            SupplierNum = doc.Bas_BonusData.Select(cred => cred.SupplierNum).FirstOrDefault(),
                            Debitor = doc.Bas_BonusData.Select(deb => deb.DebitorNum).FirstOrDefault(),
                            Manager = doc.Bas_BonusData.Select(manager => manager.ManagerName).FirstOrDefault(),
                            SupplierName = doc.Bas_BonusData.Select(supp => supp.SupplierName).FirstOrDefault(),
                            Sum = doc.Bas_BonusData.Sum(bill => bill.Price),
                            IsPrinted = doc.isPrinted,
                            IsSent = doc.isSent,
                            NotSummarize = doc.NotSummarize
                        }).OrderBy(key => key.DocumentNum)
                        .ToList()
                        .Select(q => new AcceptedBonusProjection
                        {
                            DocumentNum = q.DocumentNum,
                            DocumentDate = q.DocumentDate,
                            Description = String.Join("\n\r", q.Description.ToArray()),
                            BillsCount = q.BillsCount,
                            SupplierNum = q.SupplierNum,
                            Debitor = q.Debitor,
                            Manager = q.Manager,
                            SupplierName = q.SupplierName,
                            Sum = q.Sum,
                            IsPrinted = q.IsPrinted,
                            IsSent = q.IsSent,
                            NotSummarize = q.NotSummarize
                        });

有更好的方法来实现我的目标吗?我可以通过什么方式缩短这个查询?

最佳答案

前半部分(anony.struct)根本不需要:

var documentQuery = 
   entities.Bas_DocumentData
   .OrderBy(key => key.DocumentNum)
   .Select(q => new AcceptedBonusProjection
                        {
                            DocumentNum = q.DocumentNum,
                            /* same as in your select snipped here as it does not add value */
                            NotSummarize = q.NotSummarize
                        })
   .ToList();

备注: 请注意,您执行的一些子查询可能无法很好地扩展(从查询中的另一个表中选择,例如 doc.Bas_BonusData.Select(manager => manager.ManagerName).FirstOrDefault() ) - 这不会被转换为内部/外部连接,并将导致许多数据库查询。急切加载并不能解决这个问题! 您应该考虑使用 SQL-View/StoredProcedure/LINQ-Join 语句来更改此设置。

关于c# - 如何缩短 linq 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11083253/

相关文章:

c# - 如何使用 Control.Dispatcher.BeginInvoke 修改 GUI

c# - 如何使用带有 C# 的 Linq 在 Entity Framework 中获取多个结果集?

c# - 删除 Amazon S3 存储桶中早于 X 天的每个文件的版本

c# - 限制异步请求而不阻塞

c# - 如何让敌人在靠近时转向并向玩家移动? Unity3D

c# - Entity Framework AutoDetectChangesEnabled = false 和 .Find

c# - Linq to Sql : how to get data - one to many

c# - 确定字典是否包含一组键的全部

c# - 我如何使用 linq 作为 Microsoft 报告的数据源

c# - 从另一个列表中获取不包含某些内容的文件列表