You must Sign In to post a response.
  • Category: LINQ

    Linq query getting null value

    Hi,
    i want to get RoleName by UserId using linq query. i am getting null value
    These are IEnumerable collections.
    this is my query :

    string RoleName = (from Users in db.Users
    join UserRoles in db.UsersInRoles on Users.UserId equals UserRoles.UserId
    join roles in db.Roles on UserRoles.RoleId equals roles.RoleId
    where Users.UserId == UserId
    select roles.RoleName).SingleOrDefault();

    can any one help me what is wrong this ?
  • #769037
    You can use this syntax to Filter the null items out in you resource
     var selection3 = VSANDictionary
    .SelectMany(vsan => vsan.Value.ActiveZoneset.ZoneList)
    .Select(z => z.ContainsMemberWWPN(zonemember))
    .Where(m=> m != null)


    Useful Reference: http://stackoverflow.com/questions/2842334/linq-query-checks-for-null

  • #769038

    Hai Murali,
    The code snippet which you shared looks good. Try to find the data based on parts of the query rather than using the whole snippet at once.
    Try to get like:

    string RoleName = (from Users in db.Users
    join UserRoles in db.UsersInRoles on Users.UserId equals UserRoles.UserId
    join roles in db.Roles on UserRoles.RoleId equals roles.RoleId
    select roles.RoleName

    Is the above query returns the roles?
    If yes, then try the below one:

    string RoleName = (from Users in db.Users
    join UserRoles in db.UsersInRoles on Users.UserId equals UserRoles.UserId
    join roles in db.Roles on UserRoles.RoleId equals roles.RoleId
    where Users.UserId == UserId
    select roles.RoleName);

    Is the above query returns the records? If yes, then use your query and it should return the records. If the records are not returning, means there may be some issue which the data when performing the join operations.
    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments