如何将SQL查询转换为Lambda表达式

时间:2016-05-17 09:49:21

标签: sql asp.net-mvc lambda

这是我的SQL查询:

SELECT
  UserName,
  IsApproved
FROM aspnet_Users u
INNER JOIN aspnet_Membership m
  ON u.UserId = m.UserId

有人可以帮我解释如何使用lambda表达式编写吗?

4 个答案:

答案 0 :(得分:1)

from u in aspnet_Users 
join m in aspnet_Membership on u.UserId equals m.UserId
select new { u.UserName, m.IsApproved };

答案 1 :(得分:0)

试试这个..

from u in context.aspnet_Users 
join m in context.aspnet_Membership on u.UserId equals m.UserId select new { u.UserName, m.IsApproved};

答案 2 :(得分:0)

您可以将JOIN与Lambda Expression一起使用,例如

var result = aspnet_Users
                .Join(aspnet_Membership, 
                u => u.UserId, 
                m => m.UserId, 
                (u, m) => new
             {
                UserName = u.UserName,
                IsApproved = u.IsApproved
             }
            );

答案 3 :(得分:0)

这个查询怎么样:

选择UserName,IsApproved 来自aspnet_Users你 u.UserId = m.UserId上的内连接aspnet_Membership m 其中u.UserName = @UserName

在这种情况下,何处放置“where”? @UserName是我定义的字符串参数  public ActionResult UserActivation(string UserName) { ... }