【发布时间】:2020-11-20 17:26:39
【问题描述】:
我正在处理 DocumentFormat.xml。
我需要做一些工作以获取 where 子句所需的数据。
但是我需要在 where 子句中做同样的工作来构造所需的对象。
这似乎很浪费。
有没有更好的方法来构建它?
var rowData = registersRows
.Where(row =>
{
var cells = row.Elements<Cell>().ToList();
return included.Contains(GetCellText(cells, "A", row.RowIndex, sharedStringTableItems));
})
.Select(row =>
{
var cells = row.Elements<Cell>().ToList();
return new RegistersRow
{
StoreNumber = GetCellText(cells, "A", row.RowIndex, sharedStringTableItems),
ChannelName = GetCellText(cells, "D", row.RowIndex, sharedStringTableItems),
ChannelDisplayName = GetCellText(cells, "E", row.RowIndex, sharedStringTableItems),
PhysicalDeviceName = GetCellText(cells, "F", row.RowIndex, sharedStringTableItems),
FriendlyName = GetCellText(cells, "G", row.RowIndex, sharedStringTableItems),
DisplayNameInLabel = GetCellText(cells, "H", row.RowIndex, sharedStringTableItems),
NumberOfRegisters =
int.Parse(GetCellText(cells, "K", row.RowIndex, sharedStringTableItems))
};
}).ToList();
具体在这个例子中我需要提取StoreNumber并获取Cells两次。
【问题讨论】:
-
我没有遵循“在 where 子句中”?您的意思是在“Select” lambda 中吗?
-
是的,从
WHERE函数到SELECT函数,换句话说就是传递WHERE函数的中间结果,这样SELECT函数就不必做同样的工作了。跨度>
标签: c# linq linq-to-objects