linq lambda复合键在哪里和分组

时间:2014-10-31 17:30:32

标签: mysql linq asp.net-mvc-4 lambda

以下mysql查询的linq lambda表达式是什么

Select Count(*), Users.BusinessName From Offers
inner join Users on Users.UserId = Offers.UserId
inner join RoleUsers on RoleUsers.User_UserId = Users.UserId
inner join Roles on Roles.RoleId = RoleUsers.Role_RoleId
where Roles.RoleName = 'Seller'
group by Users.BusinessName

根据用户名称(用户是卖家的位置)计算要约数量和组数。

public static List<CountsByUser> NoOffersByUserType()
    {
        List<Offer> offers;
        using (MainDb ctx = new MainDb())
        {
            var role = ctx.Roles.FirstOrDefault(r => r.RoleName == "Seller");
            List<User> users = role.Users.ToList();
            offers= ctx.OfertaPret.Any(u => u.User == users).ToList();
            //here is the problem u.User ==users
            }
            List<CountsByUser> requests = offers.GroupBy(o => o.User.BusinessName)
                                        .Select(nl => new CountsByUser
                                        {
                                            name = nl.Key,
                                            number = nl.Count()
                                        })
                                                    .ToList();
            return requests;
        }

1 个答案:

答案 0 :(得分:0)

offers= ctx.OfertaPret.Include(u=>u.User)
                     .Where(u => u.User.Roles.Any(r => r.RoleName == "Seller")).ToList();