Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.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#_Linq_List_Select_Conditional Statements - Fatal编程技术网

C#选择具有长度条件的多个元素

C#选择具有长度条件的多个元素,c#,linq,list,select,conditional-statements,C#,Linq,List,Select,Conditional Statements,我有一个包含字符串的列表,我想知道如何选择多个元素,以使所选元素的总长度为13 我有一个想法,使用linq可能是一个很好的解决方案,但我还不是很喜欢linq 提前感谢:) 以下是我在发布文章之前尝试过的一些代码。但由于该文件包含+2500行,因此花费的时间太长 List<string> textList = new List<string>(File.ReadAllLines(@"D:\text")); List<string> newTextList = n

我有一个包含字符串的列表,我想知道如何选择多个元素,以使所选元素的总长度为13

我有一个想法,使用linq可能是一个很好的解决方案,但我还不是很喜欢linq

提前感谢:)

以下是我在发布文章之前尝试过的一些代码。但由于该文件包含+2500行,因此花费的时间太长

List<string> textList = new List<string>(File.ReadAllLines(@"D:\text"));
List<string> newTextList = new List<string>();
foreach (string x in textList)
{
    foreach (string y in textList)
    {
        if ((x + y).Length == 13)
        {
                newTextList.Add(x + " " + y);
        }
    }
}
List textList=新列表(File.ReadAllLines(@“D:\text”);
List newTextList=新列表();
foreach(文本列表中的字符串x)
{
foreach(文本列表中的字符串y)
{
如果((x+y).长度==13)
{
newTextList.Add(x+“”+y);
}
}
}
下面是代码的重写:

List<string> textList = new List<string>(File.ReadAllLines(@"D:\text"));
List<string> newTextList = new List<string>();
for (int i = 1; i <= 12; i++)
{
    List<string> list1 = new List<string>(textList.Where(x => x.Length == i));    
    List<string> list2 = new List<string>(textList.Where(x => x.Length == 13-i));
    foreach (string x in list1)
    {
        foreach (string y in list2)
        {
            newTextList.Add(x + " " + y);
        }
    }
}
List textList=新列表(File.ReadAllLines(@“D:\text”);
List newTextList=新列表();
对于(int i=1;i x.Length==i));
列表2=新列表(textList.Where(x=>x.Length==13-i));
foreach(列表1中的字符串x)
{
foreach(列表2中的字符串y)
{
newTextList.Add(x+“”+y);
}
}
}
下面有什么方法可以做这样的事情吗

List<string> list1 = new List<string>(textList.Where(x,y => x.Length + y.Lenght == 13)); 
List list1=新列表(textList.Where(x,y=>x.Length+y.Lenght==13));

感谢您的反馈。

您可以尝试以下方法:

int currentLength=0;
var items = list.TakeWhile(x => (currentLength += x.Length ) <= 13);
int currentLength=0;

var items=list.TakeWhile(x=>(currentLength+=x.Length)尝试并发布它。这是收到“提示/帮助”的最低要求你能举一个你想要实现的好例子吗?这个问题似乎离题了,因为它是对代码的请求。你需要第一组这样的元素或所有的组合?@VMAtm基本上是所有的组合。捕获
currentLength
变量警告,代码可能无法正常工作取决于框架version@Melnikovl谢谢你的评论。我不知道。请你给我一个链接,我可以阅读更多关于这方面的信息。非常感谢你。一旦我得到你的链接,我将删除我的帖子。对不起,代码很好,关闭不影响此代码