通过System.Collections.Generic.IQueryable创建System.Collections.Generic.List集合的异步方法
源代码:
public static Task<List<T>> ToListAsync<T>(this IQueryable<T> list)
{
return Task.Run(() => list.ToList());
}
public static Task<T> FirstOrDefaultAsync<T>(this IQueryable<T> list)
{
return Task.Run(() => list.FirstOrDefault());
}
public static Task<T> LastOrDefaultAsync<T>(this IQueryable<T> list)
{
return Task.Run(() => list.LastOrDefault());
}
public static Task<bool> AnyAsync<T>(this IQueryable<T> list)
{
return Task.Run(() => list.Any());
}
使用, 将list或数组转化成IQueryable类型即可
private async int Foo() {
IQueryable<int> s = Enumerable.Range(0, 10).ToList().AsQueryable();
var d = s.FirstOrDefaultAsync();
return d;
}
网友评论