I have the following linq query, where I want to return the total number of each StatusType as a list.
public async Task<ServiceResponse<IList<(StatusType StatusType, int Count)>>> GetSummaryByStatusTypeAsync()
{
Func<Task<IList<(StatusType StatusType, int Count)>>> func = async () =>
{
using (var context = _contextFactory())
{
var items = (await context.ToListAsync<Job>(x => x
.Include(y => y.Status)))
.GroupBy(x => x.Status.StatusType)
.Select(x => new
{
StatusType = x.Key,
Count = x.Count()
}).ToList();
return items;
}
};
return await this.ExecuteAsync(func);
}
However, I get the errors;
CS0266 Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: Core.StatusType StatusType, int Count>>' to 'System.Collections.Generic.IList<(Core.StatusType StatusType, int Count)>'. An explicit conversion exists (are you missing a cast?)
CS4010 Cannot convert async lambda expression to delegate type 'Task<IList<(StatusType StatusType, int Count)>>'. An async lambda expression may return void, Task or Task, none of which are convertible to 'Task<IList<(StatusType StatusType, int Count)>>'.
Select(), your're using anonymous type while the method signature claims that you need to return aValueTuple<StatusType, int>. Usex => (x.Key, x.Count())instead. - haim770Func, that's really peculiar. - DavidGInclude. Are you using EF Core? DbContext doesn't have aToListAsyncnor does it need one - it's a Unit-of-Work for very specific entities, not a database model or connection. The cause for the problem is hidden in methods you haven't posted, egExecuteAsync, thatToListAsyncand_contextFactory- Panagiotis Kanavos