LINQ to find all childs related to parents ID
Usually in many to many relationships, the related EF classes have a navigation property that represents the collection of objects associated with each item.
In your sample code, you were referencing Subject.Experiences
, but you were missing the piece that correlates the experience id correctly.
Something like this should do the trick, where id
is the selected experience id:
var subjectList = db.Subjects
.Where(s => s.Experiences.Any(experience => experience.Id == id))
.ToList();
LINQ query to group parent and child elements
you can try with Recursion
public static List<LocationViewModel> GetHierarchy(List<LinkParentChildViewModel> linkParentChildViewModels, int parentId)
{
return linkParentChildViewModels.Where(x => x.Parent.Id == parentId).Select(x => new LocationViewModel
{
Id = x.Parent.Id,
Code = x.Parent.Code,
ChildLocations = GetHierarchy(linkParentChildViewModels, x.Child.ChildLocationId)
}).ToList();
}
Call this from Main method
var result = GetHierarchy(LinksParentChild, 8);
Combine parent list & multiple child list using LINQ
Simple go through each Department object and assign its children lists:
foreach (var dept in DepartmentList) {
dept.StudentList = StudentList.Where(s => s.DepartmentId == dept.Id).ToList();
dept.ProfessorList = ProfessorList.Where(p => p.DepartmentId == dept.Id).ToList();
}
Fluent LINQ - Select a list of parents that contains a list of children where a subset of children are present
You can flip your condition, and check to make sure all of the list values are contained within the Children
collection:
var matches = parents.Where(p => myList.All(v => p.Children.Select(c => c.SomeValue).Contains(v)));
Linq select parent/parents record with matching child items
I think this would work for you :
var userByUserName = _applicationDbContext.Users.FirstOrDefault(x =>
x.NormalizedUserName == userName.ToUpper() &&
x.ClientUsers.Any(c => c.ClientId == client.Id));
Get Children common to All Parents
Since you have many-to-many relationship, it's better to base (start) the query on the resulting entity (Children
), thus avoiding the need of GroupBy
/Distinct
if you start it from the other end (Parent
).
So given
IQueryable<Parent> parents
and assuming you have access to the context, the query can be written as follows:
var query = context.Set<Children>()
.Where(c => parents.All(p => p.ParentChildrens.Select(pc => pc.ChildrenId).Contains(c.ChildrenId)))
.Select(c => new
{
Id = c.ChildrenId,
Name = c.ChildrenLocalizations.Where(cl => cl.Language == "en").Select(cl => cl.Name).FirstOrDefault()
});
which nicely translates to a single SQL.
You start with unique Children
. For requirement (2) you simply use the navigation property. The requirement (1) is more complicated (all is always harder to achieve than any), but I think the criteria
parents.All(p => p.ParentChildrens.Select(pc => pc.ChildrenId).Contains(c.ChildrenId))
quite intuitively represents child common to all parents.
Related Topics
How to Use a Class from One C# Project With Another C# Project
C# How to Split a List in Two Using Linq
Add Header and Footer for Pdf Using Itextsharp
How to Pass Multiple Arguments in Processstartinfo
Smtpexception: Unable to Read Data from the Transport Connection: Net_Io_Connectionclosed
Getting All Attributes from an Iwebelement With Selenium Webdriver
Select Multiple Records Based on List of Id'S With Linq
Httpcontext.Current.Request.Files Is Always Empty
Linq: Select an Object and Change Some Properties Without Creating a New Object
How to Loop Through Each and Every Row, Put Value in Gridview Column Cells
How to Convert String Formula to a Mathematical Formula in C#
Removing Text Between 2 Strings
Using Linq to Groupby and Sum Datatable
How to Add Double Quotes to a String That Is Inside a Variable
How to Efficiently Remove a Query String by Key from a Url
In C# What Is the Default Value of the Bytes When Creating a New Byte Array