我使用enumerable和LINQ組來分組來自DataTable的5列數據。現在我需要訪問結果中的每一列數據。通過IEnumerable循環<object>
編輯:
private IEnumerable<object> getItemsForDisplay()
{
var result = toDisplay.AsEnumerable()
.GroupBy(x => new
{
Col1 = x.Field<string>("rItem"),
Col2 = x.Field<string>("rMaterial"),
Col3 = x.Field<string>("rSpecs")
})
.Select(g => new
{
Col1 = g.Key.Col1,
Col2 = g.Key.Col2,
Col3 = g.Key.Col3,
Col4 = String.Join(", ", g.Select(row => row.Field<string>("rQuantity"))),
Col5 = String.Join(", ", g.Select(row => row.Field<string>("rOptional"))),
}).ToList();
return result;
}
//In another function
foreach (var item in result)
{
//item.tostring shows this: {"aaa","bbb","ccc","ddd","eee")
//turn it to array string or list to access "aaa".. etc etc
}
retrieveItems從哪裏來?它不應該是結果嗎? – Andrew
使用'item.Col1','item.Col2','item.Col3',... – xanatos
創建一個類而不是使用匿名類型 –