实体框架核心:包含方法问题

Mer*_*sov 4 asp.net entity-framework-core asp.net-core

有我的数据库简化模型:

public class Chat
{
public ICollection<ApplicationUser> Users {get; set;} //nav property - represents participants of a chat
}

public class ApplicationUser : IdentityUser // it represents a net-identity user; it does not have any references to chats
{...}
Run Code Online (Sandbox Code Playgroud)

因此,在控制器类中,我尝试获取聊天,例如包含当前用户作为参与者:

var user = GetUser();
_context.Chats.Where(chat => chat.Users.Contains(user)).ToList();
Run Code Online (Sandbox Code Playgroud)

此代码引发异常:

您不能将表达式... ApplicationUser用作方法“布尔值包含[ValueBuffer](System.Collections.Generic.IEnumerable`1 [Microsoft.EntityFrameworkCore.Storage.ValueBuffer ],Microsoft.EntityFrameworkCore.Storage.ValueBuffer)”

这里有什么问题?

M. *_*cki 5

您需要像这样使用Any()

 var chatsList =_context.Chats.Where(chat => chat.Users.Any(u => u.id== user.id)).ToList();
Run Code Online (Sandbox Code Playgroud)