有我的简化数据库模型:
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
{...}
因此,在 Controller 的类(class)中,我尝试进行聊天,例如将当前用户包含为参与者:
var user = GetUser();
_context.Chats.Where(chat => chat.Users.Contains(user)).ToList();
此代码引发异常:
You can not use the type of expression ...ApplicationUser for parameter's type "Microsoft.EntityFrameworkCore.Storage.ValueBuffer" of method "Boolean Contains[ValueBuffer](System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer], Microsoft.EntityFrameworkCore.Storage.ValueBuffer)"
这里有什么问题?
最佳答案
你需要使用 Any(),就像这样
var chatsList =_context.Chats.Where(chat => chat.Users.Any(u => u.id== user.id)).ToList();
关于asp.net - Entity Framework 核心 : issue with Contains method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521837/