我有简化的db模型:
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
{...}
所以,在控制器的课程中,我尝试将包含当前用户的聊天作为参与者:
var user = GetUser();
_context.Chats.Where(chat => chat.Users.Contains(user)).ToList();
此代码抛出异常:
您不能使用表达式类型... ApplicationUser for 参数的类型 &#34; Microsoft.EntityFrameworkCore.Storage.ValueBuffer&#34;方法&#34;布尔 包含[ValueBuffer](System.Collections.Generic.IEnumerable`1 [Microsoft.EntityFrameworkCore.Storage.ValueBuffer] Microsoft.EntityFrameworkCore.Storage.ValueBuffer)&#34;
这是什么问题?
答案 0 :(得分:4)
你需要使用Any(),就像这样
var chatsList =_context.Chats.Where(chat => chat.Users.Any(u => u.id== user.id)).ToList();