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"},
}

解决方案 »

  1.   


    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
       */}
      

  2.   

    不用扩展方法,linq就能搞定
       List<string[]> c = (from temp1 in a from temp2 in b select new { temp1, temp2 }).Select(t => Enumerable.Concat(t.temp1.AsEnumerable(), t.temp2.AsEnumerable()).ToArray()).ToList();
      

  3.   


    //封装成扩展方法,大概也就是这样子:
    static class Ex
    {
       public static void AddStringArray(this List<string[]> a,List<string[]> b)
    {
       a= (from x in a
           from y in b
       select x.Concat(y).ToArray())
       .ToList();
    }
    }
    //调用:
    a.AddStringArray(b);
      

  4.   


    public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                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 = a.Multiply(b);
                foreach (var item in c)
                {
                    Response.Write(string.Join(",",item)+"<br/>");
                }
            }        
        }    public static class temp
        {
            public static List<string[]> Multiply(this List<string[]> a, List<string[]> b)
            {
                List<string[]> c = new List<string[]>();
                a.ForEach((aItem) =>
                {
                    b.ForEach(bItem =>
                    {
                        c.Add((string.Join(",", aItem) + "," + string.Join(",", bItem)).Split(','));
                    });
                });
                return 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
      

  5.   

    谢谢各位。
    我不是要写扩展方法,只是想知道linq和Enumerable的扩展方法(就是Select、Where那些)之间有没有一个对应的关系。
    比如说,把一个string[] 数组strArr转化成int[] 数组intArr,可以用linq
    intArr = (from o in strArr select int.Parse(o)).ToArray();
    也可以用Select扩展方法
    intArr = strArr.Select(o => int.Parse(o)).ToArray();然后我这个需求,1楼的linq是一种写法,而我想到的是join
    c = (from x in a join y in b on 1 equals 1 select x.Concat(y).ToArray()).ToList();
    这两个linq都能实现。我想问能不能用a.Select(....)或者a.Join(....)来做同样的事情?
      

  6.   

    原来你说的是 LAMDA表达式 和 标准表达式
    1L的写法,用LAMDA写就是一个SELECTMANY
    你可以自己研究一下
      

  7.   


    已经研究一上午了。我用Join的这样写
    a.Join(b, x => x, y => y, (x, y) => x.Concat(y).ToArray(), ____).ToList();
    但不知道1 equals 1那个怎么写成IEqualityComparer
      

  8.   

    http://topic.csdn.net/u/20120717/23/6d1cd477-ec8b-4121-9f60-a46672cf3175.html
    这个贴也是你的?
      

  9.   

    两种写法,一个叫Linq表达式,一个叫Linq操作符(也就是system.linq、system.data.linq和system.xml.linq下的函数),在IL层面等价。以后不要再用什么“扩展方法”、“Lambda方法”之类的称呼Linq操作符调用方式了。Linq表达式是Linq操作符调用功能的子集。所有的Linq表达式都可以写成Linq操作符调用,并且在IL层面上没有区别。
      

  10.   

    一个显而易见的是,linq操作符并非必须使用Lambda,比如ToList()方法,就没有用到。也并非所有的扩展方法都和Linq有关,这个很好理解,你自己也能写扩展方法,放入任何命名空间下。所以应该使用官方语言来称呼,而不是发明黑话。
      

  11.   


    不是
    那请问这两个linq表达式怎么写成linq操作符:
    from x in a from y in b select x.Concat(y)
    from x in a join y in b on 1 equals 1 select x.Concat(y)
      

  12.   


    //好吧,我还是帖出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
       */}
      

  13.   

    (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))));
      

  14.   

    (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))));
      

  15.   

    呵呵,cao版说的对,这个没必要自己写啥扩展方法,linq本身就有selectmany其实这个东西还是标准解,“笛卡尔积”sql叫crossjoin,而crossjoin在linq里直接selectmany就成了a.SelectMany(p => b, (m, n) => m.Concat(n).ToArray()).ToList();至于你另外一个问题,就得看你是否理解语法树概念,不管微软如何规定那个类sql的语句,其实最终还是解析成了语法树,所以问题不是那个语句是否有方法和lamada对应,而是语法树解析过程里有没有对应解析
      

  16.   

    这两个本来就是不同的查询操作:
    一个是full join ,一个是inner join
    不可能等价转换
      

  17.   


    说对了,我确实是在找cross join的linq操作符的写法。