我编写了一个C#MVC5 Internet应用程序,并对使用.ToListAsync
LINQ表达式提出了疑问。
以下是我的代码,它适用于索引操作结果:
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);
我想为上面的代码创建一个服务功能。这是我编码的内容:
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;
}
我收到以下错误:
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?)
完全相同的代码在Index操作结果中工作正常,但在服务方法中却不行。为什么这样,我怎么能让它工作?
提前致谢
答案 0 :(得分:54)
如评论中所述,添加using System.Data.Entity
(在Entity Framework包下)以获取QueryableExtensions
。
对于.NET Core,这些方法位于Microsoft.EntityFrameworkCore