在您忽略/投票关闭这个问题之前,我认为这是一个有效的问题,因为代码清晰度是一个重要的讨论主题,它对于编写可维护的代码至关重要,我非常感谢那些以前遇到过这个问题的人的回答.
我最近遇到了这个问题,由于大量的嵌套,LINQ 查询很快就会变得非常讨厌。
以下是我提出的格式差异的一些示例(对于相同的相对不复杂的查询)
无格式
var allInventory = system.InventorySources.Select(src => new { Inventory = src.Value.GetInventory(product.OriginalProductId, true), Region = src.Value.Region }).GroupBy(i => i.Region, i => i.Inventory);
提升格式
var allInventory = system.InventorySources
.Select(src =>
new {
Inventory = src.Value.GetInventory(product.OriginalProductId, true),
Region = src.Value.Region })
.GroupBy(
i => i.Region,
i => i.Inventory);
块格式
var allInventory = system.InventorySources
.Select(
src => new
{
Inventory = src.Value.GetInventory(product.OriginalProductId, true),
Region = src.Value.Region
})
.GroupBy(
i => i.Region,
i => i.Inventory
);
列表格式
var allInventory = system.InventorySources
.Select(src => new { Inventory = src.Value.GetInventory(product.OriginalProductId, true), Region = src.Value.Region })
.GroupBy(i => i.Region, i => i.Inventory);
我想提出一个 linq 格式的标准,以便它最大限度地提高可读性和理解性,并且看起来干净和专业。到目前为止,我无法决定,所以我将问题转向这里的专业人士。