我有一组元素和记录。我想显示仅包含数组中记录的所有记录。
例如:
数组包含:[1,2,3]
记录包含:[1,2,3,4,5,6,7,8,9,10]
我只想显示1,2,3
记录。如何在 C# 中进行比较
对不起我的英语不好。
假设您使用 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;
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);
}
}