I have a Groups domain model with name
,desc
and collection of users
(belonging to the group)
I am trying to get all groups that a particular user belongs to. This is my LinQ statement:
var results = from p in AuthorizationService.UnitOfWork.Groups.FindAll()
where
(p.Users != null && p.Users.Select(u => u.Id).Contains(CurrentUser.Id))
select p.Name;
I get the following error when i try to execute the query
Cannot compare elements of type 'System.Collections.Generic.ICollection`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported.
Any help is appreciated.Thanks!
LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.
Entity Framework Core uses Language-Integrated Query (LINQ) to query data from the database. LINQ allows you to use C# (or your . NET language of choice) to write strongly typed queries. It uses your derived context and entity classes to reference database objects.
Entity Framework is an object-relational mapping (ORM) framework for connecting C# code to external databases, usually SQL Server. LINQ is a query language embedded into C# and a set of extension methods in order to make it useful.
No. Linq as in query over the data-in-memory that was loaded using your stored procedures (which means that your queries won't be translated to SQL)?
Remove the null testing for Users object, anyway it's lazy loaded, is your Users virtual? if it is, it is lazy-loaded, it's ok to remove the null testing then
var results =
from p in AuthorizationService.UnitOfWork.Groups.FindAll()
where
p.Users.Any(u => u.Id == CurrentUser.Id)
select p.Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With