我有两个清单: Listint data1 = new Listint {1,2,3,4,5}; Liststring data2 = new Liststring{"6","3"}; 我想做类似的事情 var newData = data1.intersect(data2, lambda expression); 如果data1 [index] .ToString()== data2 [index],则
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);
如果data1 [index] .ToString()== data2 [index],则lambda表达式应该返回true
在您的情况下,您需要首先通过在每个元素上调用ToString()来转换data1.如果要返回字符串,请使用此选项.
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 }