简体   繁体   English

检索其中一个项目存在于一个项目列表中的项目列表

[英]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.

相关问题 如何选择每个项目与列表中所有项目之间都存在引用的项目? - How can I select items where there exists a reference between each item and all items in a list? 是否有Linq操作从项目列表中检索特定项目,其中该项目具有应为唯一的属性的属性值? - Is there a Linq operation to retrieve specific items from a list of items where the item has a property value for a property which should be unique? 将两个列表项连接为一项 - Concatenate two list items into one item 使用自动映射器更新项目列表中的一个项目 - Using Automapper to update one item in a list of items 如何将多个列表项组合成一个项? - How to combine many list items into one item? LINQ将列表项列表加入到一个列表中 - LINQ to join a lists of list-items into one list 如何验证一个列表中的项目是否在另外两个列表中是否存在 - How to Verify if item from one list exists in any of another 2 lists 如何从列表中检索项目 <T> 其中一个属性恰好包含另一个列表中的单词? - How do I retrieve items from a List<T> where one property contains exactly the words from another list? 静态列表中的项目 <Item> () - Items in static List<Item>() 如何建模其中一个是活动项目的项目集合? - How to model a collection of items where one of them is the active item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM