![](/img/trans.png)
[英]How can I select items where there exists a reference between each item and all items in a list?
[英]Retrieve a list of items where an item exists in one of the items lists
請原諒標題有點混亂。 我有一個包含項目列表(用戶)的模型(項目)。
我想檢索所有項目,其中當前用戶是該項目的用戶列表的成員。
我試過了:
List<Project> _MemberProjects =
_Db.Projects.Where(p =>
p.Users.Contains(_User)
).ToList();
這將導致以下錯誤:
Unable to create a constant value of type 'Nimble.Models.UserAccount'. Only primitive types or enumeration types are supported in this context.
用戶模型:
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{....}
}
項目模型
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{...}
}
尚未嘗試過,但可能有效:
List<Project> _MemberProjects =
_Db.Projects.Where(p =>
p.Users.Any(u => u.UserID == _User.UserID )
).ToList();
問題是您將Linq(WHERE子句)和非Linq Collection操作(包含)混合在一起。 嘗試使用純Linq。 @JamesBond的答案可能有用。
您在查詢數據庫嗎? 然后,JOIN可能是另一個解決方案,但是確切的語法取決於您如何存儲兩個表之間的關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.