How to show assigned Departments of an user in my lambda expression (ASP.NET MVC 5)

Diego Romero 21 Reputation points
2021-12-16T12:29:29.167+00:00

Hello everyone! I'm trying to show in my view the assigned departments of logged users. I'm using Forms Authentication and i have the following tables:

158166-captura.png

I'm using the UserDepartmentMapping table to assign Users to a Department to show them in the Depatments view.

This is my Departments controller where i'm trying to show the assigned departments based on the logged user:

private MVC_DBEntities db = new MVC_DBEntities();  
  
        // GET: Departments  
        public ActionResult Index()  
        {  
            var ID = User.Identity.Name;  
            return View(db.Department.Where(x => x.User.EmailID == ID).ToList());  
        }  

But in my View, i'm getting nothing

158194-1.png

This is my Departments model

public partial class Department  
    {  
        public Department()  
        {  
            this.Product = new HashSet<Product>();  
            this.UserDepartmentMapping = new HashSet<UserDepartmentMapping>();  
        }  
      
        public int DeptId { get; set; }  
        public string DepartmentName { get; set; }  
        public Nullable<int> DepartmentBoss { get; set; }  
        public virtual ICollection<Product> Product { get; set; }  
        public virtual User User { get; set; }  
        public virtual ICollection<UserDepartmentMapping> UserDepartmentMapping { get; set; }  
    }  

And this is my UserDepartmentMapping

public partial class UserDepartmentMapping  
    {  
        public int ID { get; set; }  
        public int UserID { get; set; }  
        public int DepartmentID { get; set; }  
      
        public virtual Department Department { get; set; }  
        public virtual User User { get; set; }  
    }  

So, what's the appropriate lambda expression?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,459 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,690 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
{count} votes

Accepted answer
  1. P a u l 10,731 Reputation points
    2021-12-16T16:06:58.47+00:00

    Does this express work for you?

    db.Department.Where(d => d.UserDepartmentMapping.Any(ud => ud.User.EmailID == ID)).ToList()
    

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 28,056 Reputation points
    2021-12-16T15:02:35.987+00:00

    A direct relationship between Department and User should not exist since there is a many-to-many between the two tables with the UserDepartmentMapping.

    Get started with Entity Framework 6
    Relationships, navigation properties, and foreign keys
    Querying and Finding Entities


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.