-1

Please look at my code below

        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();

I receive not correct data in ListOfRoles - I receive data only of first user. I tried to add something like this

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

I also tried change for this

ListOfRoles = db.Users.Where(x=>x.UserId == o.UserId).Select(x=>x.AspNetUsers.AspNetRoles)

But in this case I can't select x.Name. I am doing something wrong. Please advise me.

Sathyajith
  • 3,836
  • 3
  • 15
  • 28
ewqewqewqe
  • 51
  • 2
  • 9
  • 1
    You're only asking for the roles for a single user in `ListOfRoles` - the user currently identified by the `user` range variable. And you're only asking for a single result due to using `FirstOrDefault`. Perhaps you shouldn't be calling `FirstOrDefault`? – Jon Skeet Jun 03 '17 at 20:48
  • I Removed FirstOrDefault and get error - Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation. – ewqewqewqe Jun 03 '17 at 20:57
  • 2
    Well that's a debugger issue - it doesn't say anything about what's happening in any other way. – Jon Skeet Jun 03 '17 at 21:02

1 Answers1

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();
        }
        }

This is the answer! It's work!

ewqewqewqe
  • 51
  • 2
  • 9