【发布时间】:2016-02-14 00:17:40
【问题描述】:
我有以下代码:
public class Info
{
public string Name;
public string Num;
}
string s1 = "a,b";
string s2 = "1,2";
IEnumerable<Info> InfoSrc =
from name in s1.Split(',')
from num in s2.Split(',')
select new Info()
{
Name = name,
Num = num
};
List<Info> listSrc = InfoSrc.ToList();
我希望我的 listSrc 结果包含两个 Info 项目,其 Name 和 Num 属性是:
a, 1
b, 2
但是,我上面显示的代码导致了四个项目:
a, 1
a, 2
b, 1
b, 2
【问题讨论】:
-
分割的长度是否始终相同?
-
查看
Enumerable.Zip。
标签: c# string linq list ienumerable