Querying many-To-many relationship with a list of parameters
I use EF core and i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users"). The relationship is managed via middle class "UserAuthority".
I need to query all "Users" that have "Authorities" with certain names.
I tried this:
List<string> authorities = A list of authorities;
(from user in this.dbContext.user.Include("authorities.authority")
where authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
select new UserDto(user.id, user.firstname + " " + user.lastname)).ToList()
But the console says that LINQ cant translate
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
and that it will be handled in memory.
What is the correct approach here?
entity-framework linq many-to-many entity-framework-core
add a comment |
I use EF core and i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users"). The relationship is managed via middle class "UserAuthority".
I need to query all "Users" that have "Authorities" with certain names.
I tried this:
List<string> authorities = A list of authorities;
(from user in this.dbContext.user.Include("authorities.authority")
where authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
select new UserDto(user.id, user.firstname + " " + user.lastname)).ToList()
But the console says that LINQ cant translate
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
and that it will be handled in memory.
What is the correct approach here?
entity-framework linq many-to-many entity-framework-core
add a comment |
I use EF core and i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users"). The relationship is managed via middle class "UserAuthority".
I need to query all "Users" that have "Authorities" with certain names.
I tried this:
List<string> authorities = A list of authorities;
(from user in this.dbContext.user.Include("authorities.authority")
where authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
select new UserDto(user.id, user.firstname + " " + user.lastname)).ToList()
But the console says that LINQ cant translate
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
and that it will be handled in memory.
What is the correct approach here?
entity-framework linq many-to-many entity-framework-core
I use EF core and i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users"). The relationship is managed via middle class "UserAuthority".
I need to query all "Users" that have "Authorities" with certain names.
I tried this:
List<string> authorities = A list of authorities;
(from user in this.dbContext.user.Include("authorities.authority")
where authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
select new UserDto(user.id, user.firstname + " " + user.lastname)).ToList()
But the console says that LINQ cant translate
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
and that it will be handled in memory.
What is the correct approach here?
entity-framework linq many-to-many entity-framework-core
entity-framework linq many-to-many entity-framework-core
edited Jan 1 at 14:23


Ivan Stoev
106k782135
106k782135
asked Jan 1 at 14:05
M364M4N croM364M4N cro
1741315
1741315
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Currently the only translatable in-memory collection method is Contains
(for primitive type in-memory collection, translates to SQL IN(...)
).
So instead of
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
use
user.authorities.Any(ua => authorities.Contains(ua.authority.authority))
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Well, I'm pretty sure this works. May be you have other non translatable parts, likeUserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?
– Ivan Stoev
Jan 1 at 15:42
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it withauthorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find theauthorities.ElementAt(0) == ua.authority.authority
part in them)
– M364M4N cro
Jan 1 at 20:53
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, butContains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?
– Ivan Stoev
Jan 2 at 10:34
add a comment |
You wrote:
i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users").
Something like this:
class User
{
public int Id {get; set;}
...
// every User has zero or more Authorities (many-to-many)
public virtual ICollection<Authority> Authorities {get; set;}
}
class Authority
{
public int Id {get; set;}
public string Name {get; set;}
...
// every Authority has zero or more Users (many-to-many)
public virtual ICollection<User> Users {get; set;}
}
I need to query all "Users" that have "Authorities" with certain names.
If I read this literally, you want all Users
, that have at least one Authority
that has a Name
that is in the collection of certainNames
. You want each User
with ALL his Authorities
, even those Authorities
with names that are not in certainNames
It could also mean that you want all Users
, each with only those of their Authorities
that have a Name
which is in certainNames
, but only those Users
that have at least one such Authority
.
How about this:
IEnumerable<string> certainNames = ...
var UsersWithAuthoritiesThatAreInCertainNames = myDbContext.Users
.Where (user => user.Authorities.Select(authority => authority.Name)
.Intersect(certainNames)
.Any())
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
In words:
From the collection of all users, keep only those users, that have at least one authority with a name that is also in certainNames. From the remaining users, select several properties.
If you don't want ALL Authorities of the user, but only the ones that are in certain names:
var UsersWithOnlyTheirAuthoritiesThatAreInCertainNames = myDbContext.Users
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities
.Where(authority => certainNames.Contains(authority.Name))
.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
// keep only the Users that have at least one such authority
.Where(selectedUser => selectedUser.Authorities.Any());
In words:
from the collection of Users, select some properties of every user, inclusive some properties of only those Authorities of the user that have a name that is also in certainNames. From the remaining sequence of selected users keep only those users that have at least one authority left
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996105%2fquerying-many-to-many-relationship-with-a-list-of-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Currently the only translatable in-memory collection method is Contains
(for primitive type in-memory collection, translates to SQL IN(...)
).
So instead of
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
use
user.authorities.Any(ua => authorities.Contains(ua.authority.authority))
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Well, I'm pretty sure this works. May be you have other non translatable parts, likeUserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?
– Ivan Stoev
Jan 1 at 15:42
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it withauthorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find theauthorities.ElementAt(0) == ua.authority.authority
part in them)
– M364M4N cro
Jan 1 at 20:53
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, butContains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?
– Ivan Stoev
Jan 2 at 10:34
add a comment |
Currently the only translatable in-memory collection method is Contains
(for primitive type in-memory collection, translates to SQL IN(...)
).
So instead of
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
use
user.authorities.Any(ua => authorities.Contains(ua.authority.authority))
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Well, I'm pretty sure this works. May be you have other non translatable parts, likeUserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?
– Ivan Stoev
Jan 1 at 15:42
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it withauthorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find theauthorities.ElementAt(0) == ua.authority.authority
part in them)
– M364M4N cro
Jan 1 at 20:53
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, butContains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?
– Ivan Stoev
Jan 2 at 10:34
add a comment |
Currently the only translatable in-memory collection method is Contains
(for primitive type in-memory collection, translates to SQL IN(...)
).
So instead of
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
use
user.authorities.Any(ua => authorities.Contains(ua.authority.authority))
Currently the only translatable in-memory collection method is Contains
(for primitive type in-memory collection, translates to SQL IN(...)
).
So instead of
authorities.Any(x => user.authorities.Any(y => y.authority.authority == x))
use
user.authorities.Any(ua => authorities.Contains(ua.authority.authority))
answered Jan 1 at 14:22


Ivan StoevIvan Stoev
106k782135
106k782135
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Well, I'm pretty sure this works. May be you have other non translatable parts, likeUserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?
– Ivan Stoev
Jan 1 at 15:42
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it withauthorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find theauthorities.ElementAt(0) == ua.authority.authority
part in them)
– M364M4N cro
Jan 1 at 20:53
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, butContains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?
– Ivan Stoev
Jan 2 at 10:34
add a comment |
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Well, I'm pretty sure this works. May be you have other non translatable parts, likeUserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?
– Ivan Stoev
Jan 1 at 15:42
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it withauthorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find theauthorities.ElementAt(0) == ua.authority.authority
part in them)
– M364M4N cro
Jan 1 at 20:53
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, butContains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?
– Ivan Stoev
Jan 2 at 10:34
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Doesn't really work, i still get warnings that i cant be translated...
– M364M4N cro
Jan 1 at 15:11
Well, I'm pretty sure this works. May be you have other non translatable parts, like
UserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?– Ivan Stoev
Jan 1 at 15:42
Well, I'm pretty sure this works. May be you have other non translatable parts, like
UserDto
constructor - you didn't provide the model, so I can't test myself. What does the warning say after applying the above modification?– Ivan Stoev
Jan 1 at 15:42
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it with
authorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find the authorities.ElementAt(0) == ua.authority.authority
part in them)– M364M4N cro
Jan 1 at 20:53
When i remove the manyToMany where clause, or if i remove the "contains" part and replace it with
authorities.ElementAt(0) == ua.authority.authority
, then there are no warnings (but then it generates 2 sqls for some reason and i cant find the authorities.ElementAt(0) == ua.authority.authority
part in them)– M364M4N cro
Jan 1 at 20:53
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
and i would need to send you a screenshot to show u the console, the warning massag is quite big
– M364M4N cro
Jan 1 at 20:55
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, but
Contains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?– Ivan Stoev
Jan 2 at 10:34
I'm testing on the latest 2.2, and with a similar model structure the original issue is reproduced, but
Contains
works. So something strange is happening in your environment. What EF Core version are you using? Can you provide a full repro (e.g. [mcve[)?– Ivan Stoev
Jan 2 at 10:34
add a comment |
You wrote:
i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users").
Something like this:
class User
{
public int Id {get; set;}
...
// every User has zero or more Authorities (many-to-many)
public virtual ICollection<Authority> Authorities {get; set;}
}
class Authority
{
public int Id {get; set;}
public string Name {get; set;}
...
// every Authority has zero or more Users (many-to-many)
public virtual ICollection<User> Users {get; set;}
}
I need to query all "Users" that have "Authorities" with certain names.
If I read this literally, you want all Users
, that have at least one Authority
that has a Name
that is in the collection of certainNames
. You want each User
with ALL his Authorities
, even those Authorities
with names that are not in certainNames
It could also mean that you want all Users
, each with only those of their Authorities
that have a Name
which is in certainNames
, but only those Users
that have at least one such Authority
.
How about this:
IEnumerable<string> certainNames = ...
var UsersWithAuthoritiesThatAreInCertainNames = myDbContext.Users
.Where (user => user.Authorities.Select(authority => authority.Name)
.Intersect(certainNames)
.Any())
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
In words:
From the collection of all users, keep only those users, that have at least one authority with a name that is also in certainNames. From the remaining users, select several properties.
If you don't want ALL Authorities of the user, but only the ones that are in certain names:
var UsersWithOnlyTheirAuthoritiesThatAreInCertainNames = myDbContext.Users
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities
.Where(authority => certainNames.Contains(authority.Name))
.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
// keep only the Users that have at least one such authority
.Where(selectedUser => selectedUser.Authorities.Any());
In words:
from the collection of Users, select some properties of every user, inclusive some properties of only those Authorities of the user that have a name that is also in certainNames. From the remaining sequence of selected users keep only those users that have at least one authority left
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
add a comment |
You wrote:
i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users").
Something like this:
class User
{
public int Id {get; set;}
...
// every User has zero or more Authorities (many-to-many)
public virtual ICollection<Authority> Authorities {get; set;}
}
class Authority
{
public int Id {get; set;}
public string Name {get; set;}
...
// every Authority has zero or more Users (many-to-many)
public virtual ICollection<User> Users {get; set;}
}
I need to query all "Users" that have "Authorities" with certain names.
If I read this literally, you want all Users
, that have at least one Authority
that has a Name
that is in the collection of certainNames
. You want each User
with ALL his Authorities
, even those Authorities
with names that are not in certainNames
It could also mean that you want all Users
, each with only those of their Authorities
that have a Name
which is in certainNames
, but only those Users
that have at least one such Authority
.
How about this:
IEnumerable<string> certainNames = ...
var UsersWithAuthoritiesThatAreInCertainNames = myDbContext.Users
.Where (user => user.Authorities.Select(authority => authority.Name)
.Intersect(certainNames)
.Any())
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
In words:
From the collection of all users, keep only those users, that have at least one authority with a name that is also in certainNames. From the remaining users, select several properties.
If you don't want ALL Authorities of the user, but only the ones that are in certain names:
var UsersWithOnlyTheirAuthoritiesThatAreInCertainNames = myDbContext.Users
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities
.Where(authority => certainNames.Contains(authority.Name))
.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
// keep only the Users that have at least one such authority
.Where(selectedUser => selectedUser.Authorities.Any());
In words:
from the collection of Users, select some properties of every user, inclusive some properties of only those Authorities of the user that have a name that is also in certainNames. From the remaining sequence of selected users keep only those users that have at least one authority left
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
add a comment |
You wrote:
i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users").
Something like this:
class User
{
public int Id {get; set;}
...
// every User has zero or more Authorities (many-to-many)
public virtual ICollection<Authority> Authorities {get; set;}
}
class Authority
{
public int Id {get; set;}
public string Name {get; set;}
...
// every Authority has zero or more Users (many-to-many)
public virtual ICollection<User> Users {get; set;}
}
I need to query all "Users" that have "Authorities" with certain names.
If I read this literally, you want all Users
, that have at least one Authority
that has a Name
that is in the collection of certainNames
. You want each User
with ALL his Authorities
, even those Authorities
with names that are not in certainNames
It could also mean that you want all Users
, each with only those of their Authorities
that have a Name
which is in certainNames
, but only those Users
that have at least one such Authority
.
How about this:
IEnumerable<string> certainNames = ...
var UsersWithAuthoritiesThatAreInCertainNames = myDbContext.Users
.Where (user => user.Authorities.Select(authority => authority.Name)
.Intersect(certainNames)
.Any())
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
In words:
From the collection of all users, keep only those users, that have at least one authority with a name that is also in certainNames. From the remaining users, select several properties.
If you don't want ALL Authorities of the user, but only the ones that are in certain names:
var UsersWithOnlyTheirAuthoritiesThatAreInCertainNames = myDbContext.Users
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities
.Where(authority => certainNames.Contains(authority.Name))
.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
// keep only the Users that have at least one such authority
.Where(selectedUser => selectedUser.Authorities.Any());
In words:
from the collection of Users, select some properties of every user, inclusive some properties of only those Authorities of the user that have a name that is also in certainNames. From the remaining sequence of selected users keep only those users that have at least one authority left
You wrote:
i have the classes "User" and "Authority" which are connected with a many-to-many relationship ("Users" has a property "authorities" and "Authority" has a property "users").
Something like this:
class User
{
public int Id {get; set;}
...
// every User has zero or more Authorities (many-to-many)
public virtual ICollection<Authority> Authorities {get; set;}
}
class Authority
{
public int Id {get; set;}
public string Name {get; set;}
...
// every Authority has zero or more Users (many-to-many)
public virtual ICollection<User> Users {get; set;}
}
I need to query all "Users" that have "Authorities" with certain names.
If I read this literally, you want all Users
, that have at least one Authority
that has a Name
that is in the collection of certainNames
. You want each User
with ALL his Authorities
, even those Authorities
with names that are not in certainNames
It could also mean that you want all Users
, each with only those of their Authorities
that have a Name
which is in certainNames
, but only those Users
that have at least one such Authority
.
How about this:
IEnumerable<string> certainNames = ...
var UsersWithAuthoritiesThatAreInCertainNames = myDbContext.Users
.Where (user => user.Authorities.Select(authority => authority.Name)
.Intersect(certainNames)
.Any())
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
In words:
From the collection of all users, keep only those users, that have at least one authority with a name that is also in certainNames. From the remaining users, select several properties.
If you don't want ALL Authorities of the user, but only the ones that are in certain names:
var UsersWithOnlyTheirAuthoritiesThatAreInCertainNames = myDbContext.Users
.Select(user => new
{
// select only the User properties you actually plan to use:
Id = user.Id,
Name = user.Name,
...
Authorities = user.Authorities
.Where(authority => certainNames.Contains(authority.Name))
.Select(authority => new
{
// again select only the Authority properties you plan to use:
Id = authority.Id,
Name = authority.Name,
...
})
.ToList(),
})
// keep only the Users that have at least one such authority
.Where(selectedUser => selectedUser.Authorities.Any());
In words:
from the collection of Users, select some properties of every user, inclusive some properties of only those Authorities of the user that have a name that is also in certainNames. From the remaining sequence of selected users keep only those users that have at least one authority left
answered Jan 2 at 10:43


Harald CoppoolseHarald Coppoolse
12.9k12964
12.9k12964
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
add a comment |
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
Did you miss the EF Core tag? Or forget that EF Core currently requires explicit join entity for many-to-many?
– Ivan Stoev
Jan 3 at 23:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996105%2fquerying-many-to-many-relationship-with-a-list-of-parameters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown