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"},
}
解决方案 »
- 读配置文件出现"Configuration system failed to initialize"是什么问题啊?
- C#有点问题。。。。特来请教一番!!
- 手机通过基站定位是如何实现的
- Remoting 如何注册多个类型?
- 各位大虾,晚上好,在线等急~!谁有C# .net Email 的发送代码,给小弟参考下,谢谢
- 请教各路高手一个加密方式
- 关于自动刷新问题
- 在 datagrid里面编辑,后台是acess数据库,我用OleDbDataAdapter等做保存,没成功
- C#生成.htm文件【winform】
- 请问,有关用户自定义控件的问题。急!!!
- C# 调用C++接口问题,传递参数为结构体引用,结构体中有返回数据
- 主键与唯一索引
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
*/}
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();
//封装成扩展方法,大概也就是这样子:
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);
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
我不是要写扩展方法,只是想知道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(....)来做同样的事情?
1L的写法,用LAMDA写就是一个SELECTMANY
你可以自己研究一下
已经研究一上午了。我用Join的这样写
a.Join(b, x => x, y => y, (x, y) => x.Concat(y).ToArray(), ____).ToList();
但不知道1 equals 1那个怎么写成IEqualityComparer
这个贴也是你的?
不是
那请问这两个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)
//好吧,我还是帖出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
*/}
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))));
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))));
一个是full join ,一个是inner join
不可能等价转换
说对了,我确实是在找cross join的linq操作符的写法。