LINQ ToListAsync expression with a DbSet

async-await c# entity-framework-6 ienumerable linq

Question

I have coded a C# MVC5 Internet application, and have a question about using the .ToListAsync LINQ expression.

Here is my code that works in an Index action result:

IEnumerable<IMapLocationItem> mapLocationImageGalleries = await db.mapLocationImageGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
IEnumerable<IMapLocationItem> mapLocationVideoGalleries = await db.mapLocationVideoGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
IEnumerable<IMapLocationItem> mapLocationItemsCombined = mapLocationImageGalleries.Concat(mapLocationVideoGalleries);

I am wanting to create a service function for the above code. Here is what I have coded:

public async Task<IEnumerable<IMapLocationItem>> GetAllMapLocationItemsFromUserName(string userName)
{
    IEnumerable<IMapLocationItem> mapLocationImageGalleries = await db.mapLocationImageGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
    IEnumerable<IMapLocationItem> mapLocationVideoGalleries = await db.mapLocationVideoGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
    IEnumerable<IMapLocationItem> mapLocationItemsCombined = mapLocationImageGalleries.Concat(mapLocationVideoGalleries);
    return mapLocationItemsCombined;
}

I am getting the following error:

Error   17  'System.Linq.IQueryable<CanFindLocation.Models.MapLocationItems.MapLocationImageGallery>' does not contain a definition for 'ToListAsync' and no extension method 'ToListAsync' accepting a first argument of type 'System.Linq.IQueryable<CanFindLocation.Models.MapLocationItems.MapLocationImageGallery>' could be found (are you missing a using directive or an assembly reference?)

The exact same code works fine in the Index action result, yet not in a service method. Why is this and how can I get this working?

Thanks in advance

1
26
6/29/2015 8:36:20 PM

Accepted Answer

As noted in the comments, add using System.Data.Entity (under the Entity Framework package) to get the QueryableExtensions.

For .NET Core, these methods are under Microsoft.EntityFrameworkCore

63
1/23/2017 2:58:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow