错误的LINQ请求

时间:2017-06-03 20:38:18

标签: c# sql linq

请查看下面的代码

bookAdapter.notifyDataSetChanged()

我在ListOfRoles中收到的数据不正确 - 我只接收第一个用户的数据。 我试着添加这样的东西

        var result = (from c in db.vCompanies
                      where id == c.id
                      from user in db.Users
                      select new ViewCompanyModel()
                      {
                          Id = c.id,
                          Descripton = c.Descripton,
                          Name = c.Name,
                          ImageLink = c.ImageLink,
                          AdminEmail = c.Email,
                          Users = db.eUsers
                          .Where(o => o.CompanyName.Equals(c.Name))
                          .Select(o => new UserManageViewModel.UserViewModel
                          {
                              Id = o.UserId,
                              Name = o.LastName,
                              Email = o.Email,
                              Company = o.CompanyName,
                              ListOfRoles = user.AspNetUsers.AspNetRoles.Select(x=>x.Name).ToList()
                          })
                      }).FirstOrDefault();

我也试过改变这个

Where(x=>x.UserId == o.UserId)

但在这种情况下,我无法选择x.Name。 我做错了什么。 请告诉我。

1 个答案:

答案 0 :(得分:0)

if(result != null)
        { 
        foreach (var user in result.Users)
        {
            var roles = db.Users.Where(x => x.UserId == user.Id).Select(x => x.AspNetUsers.AspNetRoles).FirstOrDefault();
            user.ListOfRoles = roles.Select(x => x.Name).ToList();
        }
        }

这就是答案!这是工作!