I have an async method. This method get list of 10 row from database and getting total count of query result. Because I'm using for grid pagination. So, I'm using tuple which one element is List and other element is count of total number. But I can't return result because of compilation error. How i can do this?
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
}
Async methods can have the following return types: Task<TResult>, for an async method that returns a value. Task, for an async method that performs an operation but returns no value. void, for an event handler. Starting with C# 7.0, any type that has an accessible GetAwaiter method.
The FromResult async method is a placeholder for an operation that returns a DayOfWeek. When GetLeisureHoursAsync is called from within an await expression in the ShowTodaysInfo method, the await expression retrieves the integer value (the value of leisureHours) that's stored in the task returned by the GetLeisureHours method.
Using .NET 4's Tuple class avoids that, but now you're stuck with non-descriptive variable names (Item1, Item2, and so on). Additionally, being a class, you have the garbage collection overhead, albeit small, of allocating the Tuple. Another way to return multiple values from a method is out parameters.
The new syntax for tuples mirrors the syntax for input parameters: a comma separated list of variable declarations inside parentheses. For example, here is the signature for a find method that searches the input for a match and returns a tuple containing both the index and item as output.
ToListAsync
returns task, remove it to return 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);
}
You have some minor errors here.
First, the return type, the int is declared outside of the tuple. Add it inside.
Then, you have mixed the await keyword. Use it where you want to wait for async operations to finish.
Try it like this
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();
query = query.ApplyPaging(queryObject);//like Skip(20).Take(10)
var students = await query.ToListAsync();
return new Tuple<List<IdNamePair>, int>(students, totalCount);
}
Extra 1: You could also use the shorter tuple declaration here
public async Task<(List<IdNamePair>, int)> GetStudents()
{
...
return (students, totalCount);
}
Extra 2: In C# 7, You could also name the tuple items which brings more clarity to the consumer of the method
public async Task<(List<IdNamePair> Students, int TotalCount)> GetStudents()
{
...
return (students, totalCount);
}
You have used await
keyword on wrong statement. This keyword is used with asynchronous
method calls only.
Sample structure for async
and await
with given return type -
public async Task<Tuple<List<string>, int>> GetTupleResultAsync()
{
List<string> listd = new List<string>() { "A", "A", "D", "E" };
//Test code to await
await Task.Run(() => "");
return new Tuple<List<string>, int>(listd, listd.Count);
}
Try below modified code -
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();
query = query.ApplyPaging(queryObject);//like Skip(20).Take(10)
var students = await query.ToListAsync();
return new Tuple<List<IdNamePair>, int>(students, totalCount); //ERROR
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With