我有两个清单:
List<int> data1 = new List<int> {1,2,3,4,5}; List<string> data2 = new List<string>{"6","3"};
我想做类似的事情
var newData = data1.intersect(data2, lambda expression);
如果满足以下条件,则lambda表达式应返回true: data1[index].ToString() == data2[index]
data1[index].ToString() == data2[index]
您需要先对data1进行转换,方法是调用ToString()每个元素。
ToString()
List<int> data1 = new List<int> {1,2,3,4,5}; List<string> data2 = new List<string>{"6","3"}; var newData = data1.Select(i => i.ToString()).Intersect(data2);
List<int> data1 = new List<int> {1,2,3,4,5}; List<string> data2 = new List<string>{"6","3"}; var newData = data1.Intersect(data2.Select(s => int.Parse(s));
请注意,如果并非所有字符串都是数字,这将引发异常。因此,您可以先进行以下检查:
int temp; if(data2.All(s => int.TryParse(s, out temp))) { // All data2 strings are int's }