【发布时间】:2014-09-20 14:45:38
【问题描述】:
我有一个包含字符串的列表,我想要一些提示/帮助如何选择多个元素,以便所选元素的总长度为例如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<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<string> list1 = new List<string>(textList.Where(x,y => x.Length + y.Lenght == 13));
感谢您的反馈。
【问题讨论】:
-
尝试并发布。这是接收“提示/帮助”的最低要求
-
你能举一个很好的例子来说明你想要达到的目标吗?
-
这个问题似乎是题外话,因为它是一个代码请求。
-
你需要第一组这样的元素还是所有的组合?
-
@VMAtm 基本上所有的组合。
标签: c# linq list select conditional-statements