c# - 如何将此 Linq 查询语法转换为方法语法?

标签 c# .net linq entity-framework

answer here包含以下查询:

var query = from role in _db.Roles
            where role.Name == roleName
            from userRoles in role.Users
            join user in _db.Users
            on userRoles.UserId equals user.Id
            select user;

如何使用 Linq 方法语法重现相同的查询?

最佳答案

var query = _db.Roles
    .Where(role => role.Name == roleName)
    .SelectMany(role => role.Users)
    .Join(_db.Users, userRole => userRole.UserId, user => user.Id, (role, user) => user);

一些解释

var query = from role in _db.Roles
        where role.Name == roleName // this will be translated to .Where
        from userRoles in role.Users // this is .SelectMany
        join user in _db.Users // this is .Join
        on userRoles.UserId equals user.Id // second and third arguments of .Join
        select user; // last argument of .Join

关于c# - 如何将此 Linq 查询语法转换为方法语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31127198/

相关文章:

c# - 如何禁用DatagridView 中的最后一个空行?

.net - 使用 NUnit 测试私有(private)方法和内部类?

.net - ASP.NET MVC View 全局化

c# - 将整数列表移动到列表的前面

c# - 从 linq 查询中获取值(value)(需要想法/建议)

c# - ASP.NET:具有快速回发的动态多提交按钮

c# - 使用 IIS 7 的 CaSTLe MonoRail 路由?

c# - 从控制台应用程序返回数据

c# - 在类似 eBay 的系统中使用的最佳搜索引擎 (.NET)

c# - 在linq查询结果中使用foreach的问题