如何使用c#查找数组中记录的存在位置

时间:2013-04-01 11:49:38

标签: linq lambda

我有一系列元素和记录。我想显示只包含数组中记录的所有记录。

例如:

数组包含:[1,2,3] 记录包含:[1,2,3,4,5,6,7,8,9,10]

我想只显示1,2,3条记录。如何在c#

中进行比较

抱歉我的英文。

2 个答案:

答案 0 :(得分:1)

假设您使用linq进行查询:

int[] array = new[] { 1,2,3 };
var record1 = new[] { 1,2,3,4,5,6,7,8,9,10 };
var record2 = new[] { 4,5,6,7,8,9,10 };
var records = new[] { record1, record2 };

// this will return record if at least one record in array is matched
var result1 = from r in records where array.Any(a => r.Contains(a)) select r;

// this will return record only if all items in array are matched
var result2 = from r in records where array.All(a => r.Contains(a)) select r;

答案 1 :(得分:0)

 string[] a={"1","2","3"};
    string[] b={"1","2","3","4","1","5","6","7","8","9","10"};
    List<string> x=new List<string>();
    foreach (string s in a)
    {
        if (b.Contains(s))
        {
            //if you only wants to display
            Console.WriteLine(s);
            // if you want it to store , add it to a list
            if(!x.Contains(s))
            x.Add(s);
        }
    }