[英]Join LINQ tables based on model definition
I want to join tables using linq, based on the definition of the model. 我想根据模型的定义使用linq联接表。 Example of what I want: 我想要的例子:
from department in User.departments... 来自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. 我正在创建一个ASP.NET MVC应用程序。 Am I doing anything wrong? 我做错什么了吗?
If the spelling of your message is exact then the reason for the error must be departments
with lower d
. 如果您的邮件拼写正确,则错误原因必须是d
较低的departments
。 Your 'Departments' property in the class definition is capital D
. 类定义中的“部门”属性为大写D
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 由于Departments
不是User class
内部的静态集合,因此存在问题,因此您需要User class
的对象才能访问Departments ICollection
属性
Since in the User class
, modify the access as follows: 由于在User class
,请如下修改访问权限:
var test = from department in Departments
Or var test = from department in Departments
或
`var test = from department in this.Departments`
Idea is same, to have object
based access instead of static
access 想法是相同的,具有基于object
的访问而不是static
访问
public bool hasDepartment(int DepartmentId, int UserId)
{
var test = from department in Departments
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.