日期:2014-05-18 浏览次数:21105 次
List<string[]> a = new List<string[]>()
{
new string[] { "aa", "bb" },
new string[] { "cc", "dd" }
};
List<string[]> b = new List<string[]>()
{
new string[] { "11" , "22"},
new string[] { "33" , "44"},
new string[] { "55" , "66"},
};
List<string[]> c = ________________
期望结果
c =
{
{"aa", "bb", "11", "22"},
{"aa", "bb", "33", "44"},
{"aa", "bb", "55", "66"},
{"cc", "dd", "11", "22"},
{"cc", "dd", "33", "44"},
{"cc", "dd", "55", "66"},
}
void Main()
{
List<string[]> a = new List<string[]>()
{
new string[] { "aa", "bb" },
new string[] { "cc", "dd" }
};
List<string[]> b = new List<string[]>()
{
new string[] { "11" , "22"},
new string[] { "33" , "44"},
new string[] { "55" , "66"},
};
List<string[]> query= (from x in a
from y in b
select x.Concat(y).ToArray()).ToList();
query.ForEach(q=> Console.WriteLine(string.Join(",",q)));
/*
aa,bb,11,22
aa,bb,33,44
aa,bb,55,66
cc,dd,11,22
cc,dd,33,44
cc,dd,55,66
*/
}
------解决方案--------------------
//好吧,我还是帖出SelectMany的写法吧
void Main()
{
List<string[]> a = new List<string[]>()
{
new string[] { "aa", "bb" },
new string[] { "cc", "dd" }
};
List<string[]> b = new List<string[]>()
{
new string[] { "11" , "22"},
new string[] { "33" , "44"},
new string[] { "55" , "66"},
};
var result=a.SelectMany(x=>b,(x,y)=>x.Concat(y).ToArray()).ToList();
// List<string[]> query= (from x in a
// from y in b
// select x.Concat(y).ToArray()).ToList();
result.ForEach(q=> Console.WriteLine(string.Join(",",q)));
/*
aa,bb,11,22
aa,bb,33,44
aa,bb,55,66
cc,dd,11,22
cc,dd,33,44
cc,dd,55,66
*/
}
------解决方案--------------------
(1)
int[] a = { 1, 2, 3 };
int[] b = { 7, 8, 9, 10 };
//var query = from x in a
// from y in b
// select new { x, y };
var query = a.SelectMany(x => b.Select(y => new { x, y }));
Console.WriteLine(string.Join("\r\n", query.Select(x => string.Format("{0}, {1}.", x.x, x.y))));
------解决方案--------------------
(2)
int[] a = { 1, 2, 3 };
int[] b = { 7, 8, 9, 10 };
//var query = from x in a
// join y in b on 1 equals 1
// select new { x, y };
var query = a.Join(b, x => 1, x => 1, (x, y) => new { x, y });
Console.WriteLine(string.Join("\r\n", query.Select(x => string.Format("{0}, {1}.", x.x, x.y))));