测试
新建一个Person
类
public class Person
{
public Person(string name,int id)
{
Name = name;
Id = id;
}
public string Name { get; set; }
public int Id { get; set; }
}
初始化List
中有一百万条数据,然后分别通过每种方法判断xiaoming
是否在List
中,代码如下
static void Main(string[] args)
{
List<Person> persons = new List<Person>();
//初始化persons数据
for (int i = 0; i < 1000000; i++)
{
Person person = new Person("My" + i,i);
persons.Add(person);
}
Person xiaoming=new Person("My999999", 999999);
//下面通过三种方法判断persons中是否包含xiaoming
Stopwatch watch = new Stopwatch();
watch.Start();
bool a = persons.Contains(xiaoming);
watch.Stop();
Stopwatch watch1 = new Stopwatch();
watch1.Start();
bool b = persons.Exists(x=>x.Id==xiaoming.Id);
watch1.Stop();
Stopwatch watch2 = new Stopwatch();
watch2.Start();
bool c = persons.Where(x=>x.Id==xiaoming.Id).Any();
watch2.Stop();
Stopwatch watch3 = new Stopwatch();
watch3.Start();
bool d = persons.Any(x => x.Id == xiaoming.Id);
watch3.Stop();
Console.WriteLine("Contains耗时:" + watch.Elapsed.TotalMilliseconds);
Console.WriteLine("Exists耗时:" + watch1.Elapsed.TotalMilliseconds);
Console.WriteLine("Where耗时:" + watch2.Elapsed.TotalMilliseconds);
Console.WriteLine("Any耗时:" + watch3.Elapsed.TotalMilliseconds);
Console.ReadLine();
}
执行结果如下图所示
结论
通过上图可以看出性能排序为
Contains > Exists > Where > Any
注意:
Contains
中不能带查询条件
LINQ系列:LINQ to SQL Exists/In/Any/All/Contains
1. Any
返回没有Product
的Category
var expr = from c in context.Categories
where !c.Products.Any()
select c;
SELECT
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName]
FROM [dbo].[Category] AS [Extent1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Product] AS [Extent2]
WHERE [Extent1].[CategoryID] = [Extent2].[CategoryID]
)
var expr = from c in context.Categories
where !c.Products.Any(p => p.UnitPrice > 10m)
select c;
SELECT
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName]
FROM [dbo].[Category] AS [Extent1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Product] AS [Extent2]
WHERE ([Extent1].[CategoryID] = [Extent2].[CategoryID]) AND ([Extent2].[UnitPrice] > cast(10 as decimal(18)))
)
2. All
var expr = from c in context.Categories
where c.Products.All(p => p.Discontinued)
select c;
SELECT
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[CategoryName] AS [CategoryName]
FROM [dbo].[Category] AS [Extent1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Product] AS [Extent2]
WHERE ([Extent1].[CategoryID] = [Extent2].[CategoryID]) AND ([Extent2].[Discontinued] <> cast(1 as bit))
)
3. Contains
var expr = from p in context.Products
where new string[]
{
"LINQ to Object",
"LINQ to ADO.NET",
"LINQ to XML"
}
.Contains(p.ProductName)
select p;
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[Discontinued] AS [Discontinued]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[ProductName] IN (N'LINQ to Object', N'LINQ to ADO.NET', N'LINQ to XML')
var expr = from p in context.Products
where !(new string[]
{
"LINQ to Object",
"LINQ to ADO.NET",
"LINQ to XML"
})
.Contains(p.ProductName)
select p;
SELECT
[Extent1].[ProductID] AS [ProductID],
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[Discontinued] AS [Discontinued]
FROM [dbo].[Product] AS [Extent1]
WHERE NOT ([Extent1].[ProductName] IN (N'LINQ to Object', N'LINQ to ADO.NET', N'LINQ to XML'))
C#中Linq的Join
比Where(m=>arr..Contains(m.id))
的效率更高
开发工程中,经常遇到在集合中,根据另一个集合取出一些数据。
我之前常用的做法如下:
List<MyInfo> list = GetList(); // 约2W数据
int[] idArr = GetIDs();// 约几百
之前也一直没有去考虑效率问题。使用如下:
list.Where(m => idArr.Contains(m.id)).ToList();
通过条件,判断集合对象的id
在数组中,逻辑没有问题,很正常的思维。(我的测试耗时约50ms)
但是,如果采用Join
就会的到成倍的速度提升,代码如下:
list.Join(idArr, m => m.id, n => n, (m, n) => m).ToList();
我们换个思路,将idArr
看做一个连接对象,用inner join
方式连接,发现耗时缩短(我的测试与5ms)
由此可见:以上的使用情况,C#中Linq的Join
比Where(m=>arr..Contains(m.id))
的效率更高。