[英]Retrieve a list of items where an item exists in one of the items lists
Please excuse the slightly confusing title. 请原谅标题有点混乱。 I have a model (Project) which contains a list of items (Users).
我有一个包含项目列表(用户)的模型(项目)。
I would like to retrieve all of the projects, where the current user is a member of the user list for that project. 我想检索所有项目,其中当前用户是该项目的用户列表的成员。
I've tried: 我试过了:
List<Project> _MemberProjects =
_Db.Projects.Where(p =>
p.Users.Contains(_User)
).ToList();
This results in the following error: 这将导致以下错误:
Unable to create a constant value of type 'Nimble.Models.UserAccount'. Only primitive types or enumeration types are supported in this context.
User Model: 用户模型:
public class UserAccount
{
public int UserID { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public ICollection<Project> Projects{....}
}
Project Model 项目模型
public class Project
{
public int ProjectID { get; set; }
public DateTime CreatedDate { get; set; }
public string ProjectName { get; set; }
public string Owner { get; set; }
public ICollection<UserAccount> Users{...}
public ICollection<ProjectGroup> Groups{...}
}
Haven't tried this, but it might work: 尚未尝试过,但可能有效:
List<Project> _MemberProjects =
_Db.Projects.Where(p =>
p.Users.Any(u => u.UserID == _User.UserID )
).ToList();
The problem is that you are mixing together Linq (the WHERE clause) and a non-Linq Collection operation (Contains). 问题是您将Linq(WHERE子句)和非Linq Collection操作(包含)混合在一起。 Try using pure Linq.
尝试使用纯Linq。 @JamesBond's answer might work.
@JamesBond的答案可能有用。
Are you querying a database? 您在查询数据库吗? Then a JOIN might be another solution, but the exact syntax depends on how you are storing the relationship between the two tables.
然后,JOIN可能是另一个解决方案,但是确切的语法取决于您如何存储两个表之间的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.