asp.net - Entity Framework 核心 : issue with Contains method

标签 asp.net asp.net-core entity-framework-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
{...}

因此,在 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/

相关文章:

sql - 带有EF Core和内存数据库提供程序的原始SQL

c# - 使用 ASP.NET Core MVC MultiSelectList 更新多对多实体

asp.net - ASP.NET 中的缓存母版页

c# - 上传到托管服务器后 Entity Framework 连接错误

asp.net-core - 在没有 Entity Framework 的情况下使用 ASP.NET Core Web API

angularjs - 使用 OpenIdConnect 和 angularjs : Bearer was forbidden 基于 Asp.net 核心 token 的声明身份验证

c# - ASP.NET Core 2.0 webApp 在发布到 iis 后显示 'view Index was not found...'

c# - 使用连接字符串 Entity Framework 在运行时生成 DbContext 和模型

asp.net - ASP.NET网站升级了VS08-VS10,现在直到我在运行时请求页面时才会显示编译时错误

asp.net - 如何关闭 ASP.NET 页面中的 SSL 连接