gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:53:55 25 4
gpt4 key购买 nike

有我的简化数据库模型:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com