Entity Framework Core: issue with Contains method

asp.net asp.net-core entity-framework-core

Question

There is my simplified model of 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
{...}

So, in controller's class I try to get chats such as contain current user as a participant:

var user = GetUser();
_context.Chats.Where(chat => chat.Users.Contains(user)).ToList();

This code throws exception:

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)"

What is the problem here?

1
4
11/10/2016 7:13:25 AM

Accepted Answer

You need use Any(), like this

 var chatsList =_context.Chats.Where(chat => chat.Users.Any(u => u.id== user.id)).ToList();
5
11/10/2016 7:48:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow