1

我正在使用 C# 并且想要一些帮助来编写一个LINQ FindAsync语句来查找MapLocation具有特定id.

我有一个DbSet<MapCompany>,每个MapCompany都有一个List<MapLocation>

我有 的idMapLocation需要写一个LINQ语句才能找到MapLocation

这是我到目前为止所拥有的:

MapLocation maplocation = await db.mapCompanies.SelectMany(mapCompany => mapCompany.mapLocations).FindAsync(id);

我可以请一些帮助来正确写这个吗?

提前致谢

4

1 回答 1

3

FindAsync()是一种方法DbSet<T>SelectMany()返回一个IQueryable<T>soFindAsync()不可用。你必须使用SingleAsync()(或者FirstAsync()如果有重复的可能性)......

MapLocation maplocation = await db.mapCompanies
                                  .SelectMany(mapCompany => mapCompany.mapLocations)
                                  .SingleAsync(l => l.Id == id);

如果你想使用FindAsync(),你必须DbSet<MapLocation>在你的 DbContext 中创建一个映射到MapLocations表的。然后你就可以使用...

MapLocation maplocation = await db.MapLocations.FindAsync(id);
于 2014-07-18T08:13:02.370 回答