0

I want to join tables using linq, based on the definition of the model. Example of what I want:

from department in User.departments...

My classes are:

USER CLASS:

public class User
{
    public User() {
        Departments = new List<Department>();
    }
    public int UserId { get; set; }
    public string UserName { get; set; }
    public virtual ICollection<Department> Departments { get; set; }
}

DEPARTMENT CLASS:

public class Department
{
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public virtual User User { get; set; }
}

I'm creating inside the user class this:

public bool hasDepartment(int DepartmentId, int UserId)
{
    var test = from department in User.departments
    //...
}

But I'm having this message: 'User' does not contain a definition for 'departments'.

I'm creating a ASP.NET MVC application. Am I doing anything wrong?

Gasper
  • 2,752
  • 1
  • 12
  • 16
Richasantos
  • 576
  • 2
  • 8
  • 15

3 Answers3

1

If the spelling of your message is exact then the reason for the error must be departments with lower d. Your 'Departments' property in the class definition is capital D.

PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
1

User' does not contain a definition for 'departments'

Issue exist since Departments is not a static collection inside User class, so you need an object of User class to access the Departments ICollection property

Since in the User class, modify the access as follows:

var test = from department in Departments Or

`var test = from department in this.Departments`

Idea is same, to have object based access instead of static access

Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
0
public bool hasDepartment(int DepartmentId, int UserId)
{
    var test = from department in Departments
    ...
}
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – ryanyuyu Sep 28 '16 at 20:52