我有一个异步方法。该方法从数据库中获取10行的列表并获取查询结果的总数。因为我正在使用网格分页。因此,我正在使用元组,其中一个元素是List而其他元素是总数的计数。但是由于编译错误,我无法返回结果。我该怎么做?
public async Task<Tuple<List<IdNamePair>, int> GetStudents(QueryFilter queryObject)
{
var query = studentEntity.Select(p => new IdNamePair
{
ID = p.ID.ToString(),
Name = p.StudentNameSurname
}).ToListAsync();
int totalCount = await query.CountAsync();
query = query.ApplyPaging(queryObject);//like Skip(20).Take(10)
var students = query.ToListAsync();
return await new Tuple<List<IdNamePair>, int>(students, totalCount); //ERROR
}
最佳答案
ToListAsync
返回任务,将其删除以返回IQueryable
public async Task<Tuple<List<IdNamePair>, int>> GetStudents(QueryFilter queryObject)
{
var query = studentEntity.Select(p => new IdNamePair
{
ID = p.ID.ToString(),
Name = p.StudentNameSurname
});
int totalCount = await query.CountAsync();
var students = await query.ApplyPaging(queryObject).ToListAsync();
return new Tuple<List<IdNamePair>, int>(students, totalCount);
}