在List中,Contains, Exists, Any都可以实现判断元素是否存在。
先上结果。性能方面:Contains 优于 Exists 优于 Any
以下为测试代码
public static void Contains_Exists_Any_Test(int num)
{
List<int> list = new List<int>();
int N = num;
for (int i = 0; i < N; i++)
{
list.Add(i);
}
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Console.WriteLine(list.Contains(N));
sw.Stop();
Console.WriteLine("Contains:"+sw.Elapsed.ToString());
sw.Start();
Console.WriteLine(list.Exists(i => i == N));
sw.Stop();
Console.WriteLine("Exists:"+ sw.Elapsed.ToString());
sw.Start();
Console.WriteLine(list.Any(i => i == N));
sw.Stop();
Console.WriteLine("Any:"+ sw.Elapsed.ToString());
}
在开发过程中可以根据实际情况进行选择,当list中数据量不大时使用Exists代码更简洁易懂;数据量大时推荐使用Contains;不推荐使用Any。
下面的代码对比就能看出为啥数据量不大的时候推荐Exists了。
class ITEM_GIDComparer : IEqualityComparer<T>
{
public bool Equals(T orl1, T orl2)
{
if (orl1==null)
{
return orl2 == null;
}
return orl1.ITEM_GID == orl2.ITEM_GID;
}
public int GetHashCode(T orl)
{
if (orl == null)
return 0;
return orl.ITEM_GID.GetHashCode();
}
}
orlclst.Contains(orlc, new ITEM_GIDComparer())
//Exists一行代码就可以实现上面的功能
orlclst.Exists(x=>x.ITEM_GID==orlc.ITEM_GID)
C#比较两个list集合,两集合同时存在或A集合存在B集合中无
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
static void Main(string[] args)
{
var student1 = new List<student>();
student1.Add(new student() { name = "张三", subject = "英语", score = 89 });
student1.Add(new student() { name = "李四", subject = "英语", score = 95 });
student1.Add(new student() { name = "王五", subject = "英语", score = 69 });
student1.Add(new student() { name = "李倩", subject = "英语", score = 99 });
var student2 = new List<student>();
student2.Add(new student() { name = "李四", subject = "英语", score = 95 });
student2.Add(new student() { name = "王五", subject = "数学", score = 69 });
student2.Add(new student() { name = "赵六", subject = "数学", score = 100 });
//var exp1 = student1.Where(a => student2.Any(t => a.name.Contains(t.name))).ToList();
//使用Exists同样可以实现 字面上应该更好理解,而且效率要高些
var exp1 = student1.Where(a => student2.Exists(t => a.name.Contains(t.name))).ToList();
Console.WriteLine("--查找student1和student2总同时存在的数据--");
foreach (var item in exp1)
{
Console.WriteLine("{0} \t {1} \t {2}", item.name, item.subject, item.score);
}
//var exp2 = student1.Where(a => student2.All(t => !a.name.Contains(t.name))).ToList();
//使用Exists同样可以实现 字面上应该更好理解,而且效率要高些
var exp2 = student1.Where(a => !student2.Exists(t => a.name.Contains(t.name))).ToList();
Console.WriteLine("--查找student1集合中存在,而student2不存在的数据--");
foreach (var item in exp2)
{
Console.WriteLine("{0} \t {1} \t {2}", item.name, item.subject, item.score);
}
var exp3 = student2.Where(a => !student1.Exists(t => a.name.Contains(t.name))).ToList();
Console.WriteLine("--查找student2集合中存在,而student1不存在的数据--");
foreach (var item in exp3)
{
Console.WriteLine("{0} \t {1} \t {2}", item.name, item.subject, item.score);
}
Console.Read();
}
public class student
{
/// <summary>
/// 姓名
/// </summary>
public string name;
/// <summary>
/// 科目
/// </summary>
public string subject;
/// <summary>
/// 分数
/// </summary>
public int score;
}
}
}