Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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# 循环列表字符串,条件在提取C上的顺序相同#_C#_List_For Loop_Foreach - Fatal编程技术网

C# 循环列表字符串,条件在提取C上的顺序相同#

C# 循环列表字符串,条件在提取C上的顺序相同#,c#,list,for-loop,foreach,C#,List,For Loop,Foreach,我有一个列表字符串列表: List<List<string>> donnees = new List<List<string>>(); 我想得到如下列表值: <value> "a", "1", "first" </value> <value> "b", "2", "second" </value> <value> "b", "3", "third" </value> &l

我有一个列表字符串列表:

  List<List<string>> donnees = new List<List<string>>();
我想得到如下列表值:

<value> "a", "1", "first" </value>
<value> "b", "2", "second" </value>
<value> "b", "3", "third" </value>
<value> "d", "4", "fourth" </value>
“a”、“1”、“第一”
“b”,“2”,“第二”
“b”,“3”,“3”
“d”、“4”、“4”
(事实上:获取相同顺序的每个列表的所有值)

当然,我有一个非常大的清单

我用这个测试

foreach (List<string> list in donnees)
{
foreach (string s in list)
{
  //Here to get the XElement with the values 
  //It is OK
}
}
foreach(在donnees中列出)
{
foreach(列表中的字符串s)
{
//在这里获取包含值的XElement
//没关系
}
}
但这不是目的,
那我该怎么修呢?谢谢,

只需通过索引进行迭代即可:

var result = new List<(string, string, string)>();
for(int i = 0; i < donnees[0].Count; i++)
{
    result.Add((donnes[0][i], donnees[1][i], donnees[2][i]));
}
var result=newlist();
对于(int i=0;i
LINQ:

donnees.Select(d =>
{
   string[] items = d.Select(i => "\" + i + "\")).ToArray();
   return $"<value>{string.Join(", ", items)}</value>");
}
donnees.选择(d=>
{
string[]items=d.Select(i=>“\”+i+“\”).ToArray();
返回$“{string.Join(“,”,items)}”);
}

您实际上是在尝试按列读取列表,然后合并结果。您可以使用嵌套循环来执行此操作

以下方法将帮助您创建所需的
XElement

public static IEnumerable<XElement> GetList(List<List<string>> source)
{
    var maxIndex= source.Max(x=>x.Count());
    var index = 0;

    while(index<maxIndex)
    {
        var lineList = new List<string>();
        foreach(var list in source.Select(x=>x))
        {
            if(list.Count > index)
                lineList.Add($"\"{list[index]}\"");
        }
        index++;
        yield return new XElement("value", string.Join(",",lineList));
    }
}
公共静态IEnumerable GetList(列表源)
{
var maxIndex=source.Max(x=>x.Count());
var指数=0;
while(indexx))
{
如果(list.Count>索引)
添加($“\”{list[index]}\”);
}
索引++;
返回新的XElement(“value”,string.Join(“,”,lineList));
}
}

我们假设每个列表都有相同的元素。如果是这样,只需根据第一个内部列表中的元素数量使用for循环,然后使用索引获得每个列表中的值/\和相同数量的元素?还有,您得到的结果是什么?使用相同的示例数据,您能告诉我们您的结果是什么吗?谢谢大家,@Anu Viswan找到解决方案这不是假设每个列表只有三个元素吗?@CaseyCrokston当然。不过,我不确定这是好事还是坏事。虽然这个答案可能是正确的,但你应该通过解释这段代码的作用以及它是如何解决原始问题来提高答案的质量——这有助于阅读你答案的其他用户了解发生了什么。
public static IEnumerable<XElement> GetList(List<List<string>> source)
{
    var maxIndex= source.Max(x=>x.Count());
    var index = 0;

    while(index<maxIndex)
    {
        var lineList = new List<string>();
        foreach(var list in source.Select(x=>x))
        {
            if(list.Count > index)
                lineList.Add($"\"{list[index]}\"");
        }
        index++;
        yield return new XElement("value", string.Join(",",lineList));
    }
}