在2个ArrayList之间查找相同的值并作为另一个ArrayList返回 [英] Find Same Values Between 2 ArrayList And Return As Another ArrayList

查看:178
本文介绍了在2个ArrayList之间查找相同的值并作为另一个ArrayList返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个数组列表共享的值提取到另一个数组列表中.

using System.Linq;

private ArrayList GetSameOf2AL(ArrayList first, ArrayList second)
        {
            ArrayList same = new ArrayList();

            var one = from int i in first select i;
            var two = from int i in second select i;

            var SameVal = one.Intersect(two);
            //I am supposed to convert or cast SameVal into arraylist here
            return same;
        }

我的问题是:

  1. 我无法将var类型转换回arraylist,有人可以建议我怎么做吗?
  2. 我是不是首先选择了错误的方法?感谢您的建议.
  1. I couldn't convert the var type back into arraylist, can someone advise me how?
  2. Did I choose a wrong method to do this at the first place? Your advise is appreciated.

谢谢大家的关注=)

推荐答案

List<int> intersection = first.Cast<int>().Intersect(second.Cast<int>()).ToList();

ArrayList intersection = new ArrayList();
foreach (var i in first.Cast<int>().Intersect(second.Cast<int>()))
    intersection.Add(i);

这篇关于在2个ArrayList之间查找相同的值并作为另一个ArrayList返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆