通过IQueryable创建List泛型集合的异步方法

通过System.Collections.Generic.IQueryable创建System.Collections.Generic.List集合的异步方法

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ReflectionIT.Linq {

static class Utils {

/// <summary>
/// 通过System.Collections.Generic.IQueryable<T>.创建System.Collections.Generic.List<T>的异步方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static Task<List<T>> ToListAsync<T>(this IQueryable<T> list) {
return Task.Run(() => list.ToList());
}

}
}

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private async void Foo() {

using (var db = new Database1Entities()) {

var qry = await (from emp in db.Employees
where emp.Salary > 1
select emp).ToListAsync();


foreach (var item in qry) {
Debug.WriteLine(item.Name);
}

}

}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×