当前位置: 代码迷 >> ASP.NET >> c#取出两个数组中相同的值?解决思路
  详细解决方案

c#取出两个数组中相同的值?解决思路

热度:1065   发布时间:2013-02-25 00:00:00.0
c#取出两个数组中相同的值?
怎么取出下面c#取出两个数组中相同的值 ?
string[] A= "朱雅君,姚怡尔,涂静";
string[] B= "姚怡尔,涂静,刘刚";

------解决方案--------------------------------------------------------
一个个循环对比 这是我的第一想法
------解决方案--------------------------------------------------------
var same=A.Intersect(B).ToArray();
------解决方案--------------------------------------------------------
http://jrunner.blog.51cto.com/1015356/655708
------解决方案--------------------------------------------------------
探讨

var same=A.Intersect(B).ToArray();

------解决方案--------------------------------------------------------
C# code
 string[] A = new string[] { "朱雅君", "姚怡尔", "涂静" };            string[] B = new string[] { "姚怡尔", "涂静", "刘刚" };            var re = (from a in A select a).Intersect(from b in B select b).ToArray();
------解决方案--------------------------------------------------------
C# code
            string[] A = new string[]{"朱雅君","姚怡尔","涂静"};            string[] B = new string[]{"姚怡尔","涂静","刘刚"};            var vs = from s in A where B.Contains(s) select s;            foreach (var v in vs)            {                MessageBox.Show(v.ToString());            }
------解决方案--------------------------------------------------------
linq就是好用。。简单又清晰
------解决方案--------------------------------------------------------
探讨

var same=A.Intersect(B).ToArray();

------解决方案--------------------------------------------------------
探讨

引用:

var same=A.Intersect(B).ToArray();

你们都是LINQ狂

------解决方案--------------------------------------------------------
Linq非常方便。我给你们写个原始点的吧。。
C# code
      string[] A = {"朱雅君","姚怡尔","涂静"};            string[] B = {"姚怡尔","涂静","刘刚"};                       foreach (var item in A)            {                foreach (var item2 in B)                {                    if (item == item2)                        return item2;                }               }
  相关解决方案