var m = proplist.Aggregate(Enumerable.Repeat("", 1), (current, item) => current.SelectMany(n => item, (a, b) => a + b)); foreach (var item in m) { Console.WriteLine(item); }
protected void Page_Load(object sender, EventArgs e) { List<List<string>> proplist = new List<List<string>>(); proplist.Add(new List<string>() { "a", "b", "r" }); proplist.Add(new List<string>() { "c", "d", "h" }); proplist.Add(new List<string>() { "e", "f", "y" }); var a = AAAAA(proplist); foreach (var temp in a) { Response.Write(temp + "<br/>"); } } public IEnumerable<string> AAAAA(List<List<string>> list) { IEnumerable<string> result = list.First(); for (int i = 1; i < list.Count; i++) { int index = i; result = result.SelectMany(x => list[index].Select(y => y + x)); } return result; } 多少个都行
来一个递归版本的. List<string> res = MergeList(proplist).ToList();private IEnumerable<string> MergeList(List<List<string>> proplist) { if (proplist.FirstOrDefault() == null) return new List<string>() { "" }; var divs = proplist.Where(c => c != proplist.FirstOrDefault()).ToList(); return from a in proplist.First() from b in MergeList(divs) select string.Format("{0}{1}", a, b); }
{
List<List<string>> proplist = new List<List<string>>(); proplist.Add(new List<string>(){"a","b","r"});
proplist.Add(new List<string>() { "c", "d","h" });
proplist.Add(new List<string>() { "e", "f","y" });[code=csharp]
var q=from a in proplist.First()
from b in proplist.Skip(1).First()
from c in proplist.Skip(2).First()
select string.Format("{0}{1}{2}",a,b,c);
q.ToList().ForEach(x=>Console.WriteLine(x));
}[/code][code=csharp]void Main()
{
List<List<string>> proplist = new List<List<string>>(); proplist.Add(new List<string>(){"a","b","r"});
proplist.Add(new List<string>() { "c", "d","h" });
proplist.Add(new List<string>() { "e", "f","y" });[code=csharp]acf
acy
ade
adf
ady
ahe
ahf
ahy
bce
bcf
bcy
bde
bdf
bdy
bhe
bhf
bhy
rce
rcf
rcy
rde
rdf
rdy
rhe
rhf
rhy [/code]
{
List<List<string>> proplist = new List<List<string>>(); proplist.Add(new List<string>(){"a","b","r"});
proplist.Add(new List<string>() { "c", "d","h" });
proplist.Add(new List<string>() { "e", "f","y" });
var q=from a in proplist.First()
from b in proplist.Skip(1).First()
from c in proplist.Skip(2).First()
select string.Format("{0}{1}{2}",a,b,c);
q.ToList().ForEach(x=>Console.WriteLine(x));
}ace
acf
acy
ade
adf
ady
ahe
ahf
ahy
bce
bcf
bcy
bde
bdf
bdy
bhe
bhf
bhy
rce
rcf
rcy
rde
rdf
rdy
rhe
rhf
rhy
var m = proplist.Aggregate(Enumerable.Repeat("", 1),
(current, item) => current.SelectMany(n => item, (a, b) => a + b)); foreach (var item in m)
{
Console.WriteLine(item);
}
就是我的proplist的item是动态添加的,不是固定只有3个的
看到需要依次处理的,考虑Aggregate
protected void Page_Load(object sender, EventArgs e)
{
List<List<string>> proplist = new List<List<string>>(); proplist.Add(new List<string>() { "a", "b", "r" });
proplist.Add(new List<string>() { "c", "d", "h" });
proplist.Add(new List<string>() { "e", "f", "y" });
var a = AAAAA(proplist);
foreach (var temp in a)
{
Response.Write(temp + "<br/>");
}
}
public IEnumerable<string> AAAAA(List<List<string>> list)
{
IEnumerable<string> result = list.First();
for (int i = 1; i < list.Count; i++)
{
int index = i;
result = result.SelectMany(x => list[index].Select(y => y + x));
}
return result;
}
多少个都行
List<string> res = MergeList(proplist).ToList();private IEnumerable<string> MergeList(List<List<string>> proplist)
{
if (proplist.FirstOrDefault() == null)
return new List<string>() { "" };
var divs = proplist.Where(c => c != proplist.FirstOrDefault()).ToList();
return from a in proplist.First() from b in MergeList(divs) select string.Format("{0}{1}", a, b);
}