C# 用LINQ动态投影 鉴于
(1) 存储为列表列表的数据库表。表的行和列大小未定义C# 用LINQ动态投影 鉴于,c#,linq,projection,C#,Linq,Projection,(1) 存储为列表列表的数据库表。表的行和列大小未定义 List<List<string>> table = new List<List<string>>(); 当前解决方案 我能想到的最好方法是迭代所有行,如下所示: List<List<string>> subtable = new List<List<string>>(); for (int index = 0; index < tabl
List<List<string>> table = new List<List<string>>();
当前解决方案
我能想到的最好方法是迭代所有行,如下所示:
List<List<string>> subtable = new List<List<string>>();
for (int index = 0; index < table.Count; index++)
{
subtable.Add(table[index].Where((t, i) => indexes.Contains(i)).ToList());
}
List<List<string>> table = new List<List<string>>();
table.Add(new List<string>() { "a1", "b1", "c1", "d1", "e1" });
table.Add(new List<string>() { "a2", "b2", "c2", "d2", "e2" });
table.Add(new List<string>() { "a3", "b3", "c3", "d3", "e3" });
List<int> indexes = new List<int>() { 1, 3, 4 };
for (int index = 0; index < table.Count; index++)
{
foreach (var columnIndex in indexes)
Console.Write(table[index][columnIndex] +" ");
Console.WriteLine();
}
Console.ReadLine();
List subtable=new List();
for(int index=0;indexindex.Contains(i)).ToList();
}
要求
如果可能的话,一个更优雅的解决方案。这个呢:
List<List<string>> subtable =
table.Select(row => indexes.Select(i => row[i]).ToList()).ToList();
列表子表=
table.Select(row=>index.Select(i=>row[i]).ToList()).ToList();
如果需要检查数组边界,可以执行以下操作:
List<List<string>> subtable =
table.Select(row => indexes.Where(i => i >= 0 && i < row.Count)
.Select(i => row[i]).ToList()).ToList();
列表子表=
table.Select(行=>index.Where(i=>i>=0&&irow[i]).ToList()).ToList();
或者,如果您更喜欢查询语法:
List<List<string>> subtable =
(from row in table
select
(from i in indexes
where i >= 0 && i < row.Count
select row[i]
).ToList()
).ToList();
列表子表=
(从表中的第行开始)
选择
(从索引中的i开始)
其中i>=0&&i
选择所有行,然后针对每一行筛选出不在索引列表中的列:
var subtable = table
.Select(row => row.Where((value, colIndex) => indexes.Contains(colIndex)))
.ToList();
如果您只想打印,不需要这样的查询就可以更简单(也更高效):
List<List<string>> subtable = new List<List<string>>();
for (int index = 0; index < table.Count; index++)
{
subtable.Add(table[index].Where((t, i) => indexes.Contains(i)).ToList());
}
List<List<string>> table = new List<List<string>>();
table.Add(new List<string>() { "a1", "b1", "c1", "d1", "e1" });
table.Add(new List<string>() { "a2", "b2", "c2", "d2", "e2" });
table.Add(new List<string>() { "a3", "b3", "c3", "d3", "e3" });
List<int> indexes = new List<int>() { 1, 3, 4 };
for (int index = 0; index < table.Count; index++)
{
foreach (var columnIndex in indexes)
Console.Write(table[index][columnIndex] +" ");
Console.WriteLine();
}
Console.ReadLine();
List table=新列表();
表.增列(新的清单{“a1”、“b1”、“c1”、“d1”、“e1”});
添加(新列表({“a2”、“b2”、“c2”、“d2”、“e2”});
添加(新列表({“a3”、“b3”、“c3”、“d3”、“e3”});
列表索引=新列表(){1,3,4};
for(int index=0;index
这正是我想要的。非常感谢。
var subtable = table
.Select(row => row.Where((value, colIndex) => indexes.Contains(colIndex)))
.ToList();
List<List<string>> table = new List<List<string>>();
table.Add(new List<string>() { "a1", "b1", "c1", "d1", "e1" });
table.Add(new List<string>() { "a2", "b2", "c2", "d2", "e2" });
table.Add(new List<string>() { "a3", "b3", "c3", "d3", "e3" });
List<int> indexes = new List<int>() { 1, 3, 4 };
for (int index = 0; index < table.Count; index++)
{
foreach (var columnIndex in indexes)
Console.Write(table[index][columnIndex] +" ");
Console.WriteLine();
}
Console.ReadLine();