Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询交错数组中所有列中的第一个元素 int numPics=3//从查询中填充 string[]picID=新字符串[numPics]; //pictureTable的构造使其与pictureTable[列][行]相关 字符串[][]pictureTable=null//假设表中有数据 对于(int i=0;ip[0]。等于(“ID”)。选择(p=>p[i]); }_C#_Linq_Jagged Arrays - Fatal编程技术网

C# 查询交错数组中所有列中的第一个元素 int numPics=3//从查询中填充 string[]picID=新字符串[numPics]; //pictureTable的构造使其与pictureTable[列][行]相关 字符串[][]pictureTable=null//假设表中有数据 对于(int i=0;ip[0]。等于(“ID”)。选择(p=>p[i]); }

C# 查询交错数组中所有列中的第一个元素 int numPics=3//从查询中填充 string[]picID=新字符串[numPics]; //pictureTable的构造使其与pictureTable[列][行]相关 字符串[][]pictureTable=null//假设表中有数据 对于(int i=0;ip[0]。等于(“ID”)。选择(p=>p[i]); },c#,linq,jagged-arrays,C#,Linq,Jagged Arrays,我是LINQ的新手,但我一直在寻找,还没有找到答案。我希望能够使用LINQ检查pictureTable中每列的第一个字符串,看看它是否与字符串匹配。然后,我想获取该列并从0到I的每一行中提取数据。我知道我可以通过改变列并保持行不变来实现for循环,但是我想使用LINQ来实现相同的结果 另外,如果有可能摆脱第一个for循环并获得相同的结果,我也会对此感兴趣 编辑:假设我们有一个包含以下数据的表,请记住所有内容都是字符串 Column Name [ID] [Name] [Age] Row

我是LINQ的新手,但我一直在寻找,还没有找到答案。我希望能够使用LINQ检查pictureTable中每列的第一个字符串,看看它是否与字符串匹配。然后,我想获取该列并从0到I的每一行中提取数据。我知道我可以通过改变列并保持行不变来实现for循环,但是我想使用LINQ来实现相同的结果

另外,如果有可能摆脱第一个for循环并获得相同的结果,我也会对此感兴趣

编辑:假设我们有一个包含以下数据的表,请记住所有内容都是字符串

Column Name    [ID]  [Name]  [Age]
Row 1           [1]   [Jim]   [25]   
Row 2           [2]   [Bob]   [30]
Row 3           [3]   [Joe]   [35]
我希望能够查询列名称,然后能够通过索引或查询行数据从中获取数据。我将给出一个使用for循环实现我想要的功能的示例

        string[][] table = new string[][] { 
                              new string[] { "ID", "Name", "Age" }, 
                              new string[] { "1",  "Jim",  "25" },
                              new string[] { "2",  "Bob",  "30" },
                              new string[] { "3",  "Joe",  "35" }};

        string[] resultRow = new string[table.GetLength(1)];
        for (int i = 0; i < table.GetLength(0); i++)
        {
            if (table[i][0] == "Name") //Given this in a LINQ Query
            {
                Console.WriteLine("Column Name = {0}\n", table[i][0]);
                for (int j = 1; j < table.GetLength(1); j++) //starts at 1
                {
                    resultRow[i] = table[i][j]; //This is what I want to happen.
                }
                break; //exit outer loop
            }
        }
        //Output:
        //Column Name = Name
string[][]表=新字符串[][]{
新字符串[]{“ID”,“Name”,“Age”},
新字符串[]{“1”,“Jim”,“25”},
新字符串[]{“2”,“Bob”,“30”},
新字符串[]{“3”,“Joe”,“35”};
string[]resultRow=新字符串[table.GetLength(1)];
for(int i=0;i
是否要将字符串连接在一起?如果是这样,您可以这样做:

picID[i] = string.Concat(pictureTable.Where(p => p[0].Equals("ID")).Select(p => p[i]));

我认为这将为您提供与您在resultRow数组中查找的内容相同的内容

string[][] table = new string[][] { 
    new string[] { "ID", "Name", "Age" }, 
    new string[] { "1", "Jim", "25" },
    new string[] { "2", "Bob", "30" },
    new string[] { "3", "Joe", "35" }
};

//get index of column to look at
string columnName = "Name";
var columnIndex = Array.IndexOf(table[0], columnName, 0);

// skip the title row, and then skip columns until you get to the proper index and get its value
var results = table.Skip(1).Select(row => row.Skip(columnIndex).FirstOrDefault());

foreach (var result in results)
{
    Console.WriteLine(result);
}

另一个需要注意的事项是SelectMany,因为您可以使用它将多个列表展平为一个列表。

您可以提供示例输入和所需输出吗?您所说的“从每行提取数据”是什么意思?请准确解释。什么是
numPics
?什么是
picID
?你的代码不完整,问题也不清楚。是的,很抱歉。我希望我用我的示例和示例数据澄清了一切。请将示例数据放入代码中。您的数据看起来是什么样子还不清楚。Steves答案中的数据正确吗?但这不起作用,它说它是IEnumerable,不能隐式地将其转换为字符串。