读书人

求1Enumerable扩展方法的写法

发布时间: 2012-08-13 13:21:53 作者: rapoo

求一Enumerable扩展方法的写法

C# code
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"},}


[解决办法]
C# code
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   */}
[解决办法]
C# code
//好吧,我还是帖出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))));

读书人网 >C#

热点推荐