LINQ Query - how sort and filter on eager fetch - linq

How do I do a eager query of a parent child relationship that:
filters a on child fields
sorts on both parent and child
return a List or Parents with the children pre-populated
If I try
from p in _context.Parents.Include("children")
join c in _context.childrenon p.Id equals c.ParentId
where d.DeletedDate == null
orderby p.Name ascending, c.Name
select p
Then I get the Parent object back but each Parent has NULL for children
if I try
from p in _context.Parents.Include("children")
orderby p.Name ascending
select p
The query returns all Parents and children but they are not filtered or sorted.
The result I want back is a IEnumerable<Parent>
i.e.
Parent[0].name = "foo"
Parent[0].children = IEnumerable<Child>
Parent[1].name = "bar"
Parent[1].children = IEnumerable<Child>

There is no direct way of doing this, but you can use somewhat of a workaround - project the parent and children onto an annonymous object and then select and return the parent from the object.
See similar question: Linq To Entities - how to filter on child entities
In your case you will have something along the lines of:
var resultObjectList = _context.
Parents.
Where(p => p.DeletedDate == null).
OrderBy(p => p.Name).
Select(p => new
{
ParentItem = p,
ChildItems = p.Children.OrderBy(c => c.Name)
}).ToList();
List<Parent> resultingCollection = resultObjectList.Select(o => o.ParentItem).ToList();

The solution depends on what exactly you are trying to do.
The first query gives the impression that you want to "flatten out" the results in objects, like this (pseudocode, I hope it's clear what I mean):
{ Parent1, Child1 }
{ Parent1, Child2 }
{ Parent1, Child3 }
{ Parent2, Child1 }
In this case each result "row" would be an object having a Parent and a Child property, and you could sort by parent name and then by child name.
The second query just returns the Parent objects and (you don't show it but I assume EF has been instructed to do that) each one has a Children collection. In this case you can only sort by parent name; if you want to sort each Parent's children, sort the Children collection on that object by itself.
Which of the two do you want to do?
Update
OK, it seems you want the second one. I don't believe it can be done directly. You can just do it when you enumerate the results - since the Parents are already sorted, simply sort each one's children:
var sortedChildren = parent.Children.OrderBy(c => c.Name);

prefetching child fields:
using (BlogDataContext context = new BlogDataContext())
{
DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Blog>(c => c.Categories);
options.LoadWith<Blog>(c => c.Title);
context.LoadOptions = options;
Blog blog = context.Blogs.Single<Blog>(c => c.BlogId == 1);
}

Related

LINQ Left Outer Join with Greater Than and Less Than Date Conditions

I've been struggling with this for a while and can't find the syntax for a LINQ outer join that has multiple conditions based on date. I've been looking into the GroupJoin syntax, but that only let's you compare one field value (normally IDs).
I would like to test if the parent table has a date (e.g. "UpdateDate") that falls within multiple values defined in the child table (e.g. "StartDate" and "EndDate"). If the parent date fits the condition, pull a column or two from the child table. If not, those columns from the child table should be null (classic left join stuff).
I don't think query syntax will work because it only recognizes equijoins.
Is there a way to do this in LINQ using Lambda syntax? I've been trying to use some combination of "SelectMany" and "DefaultIfEmpty" but keep getting stuck trying to define the join.
The way to do this in linq:
var q = from a in TableA
from b in TableB.where(x => a.Date > x.StartDate && a.Date < x.EndDate).DefaultIfEmpty()
select {...}
Use parameter ResultSelector of Queryable.GroupJoin to select what you want:
var result = dbContext.Parents.GroupJoin(dbContext.Children,
// outer and inner key Selectors:
parent => parent.Id, // from every parent take the primary key
child => child.ParentId, // from every child take the foreign key to parent
// ResultSelector: take the parent and all his children to make one new object
(parent, children) => new
{
// Select only the Parent properties you actually plan to use:
Id = parent.Id,
Name = parent.Name,
...
Children = children.Select(child => new
{
// select only Child properties you plan to use:
Id = child.Id,
// No need: you know the value: ParentId = child.ParentId,
...
"If the parent date fits the condition, pull a column or two from the child table, otherwise those columns from the child table should be null "
SpecialColumnA = (parent.BirthDay.Year < 2000) ?? child.BirthDay : null,
SpecialColumnB = (parent.Name == "Kennedy" ?? child.Name : null,
});
If the conditions are the same for a lot of columns, consider to check this only once:
SpecialColumns = (parent.Birthday.Year >= 2000) ? null :
// else fill the special columns:
new
{
Name = child.Name,
SomeWeirdProperty = parent.Id + child.Id,
...
},
});

LINQ - Where a list contains an element of another list

I have two SQL tables: Movies and Tags, while Movies contains a List<Tag>
Now I want to find all movies that have at least one of the tags of a List<Tag> argument. How can I do that in LINQ?
public IEnumerable<Group<Genre, Movie>> GetMoviesGrouped(string search, List<Tag> tags)
{
var movies = from movie in Movies
where ( movie.Name.Contains(search)) && movie.Tags.???contains any element of tags???
group movie by movie.genre into g
select new Group<Genre, Movie> { Key = g.Key, Values = g };
....
}
Movies where any of the tags is contains in tags:
var movies = from movie in Movies
where ( movie.Name.Contains(search))
&& movie.Tags.Any(t => tags.Contains(t))
group movie by movie.genre into g
select new Group<Genre, Movie> { Key = g.Key, Values = g };
However since this is comparing Tag instances and not strings:
It probably won't get translated to SQL (which means you will have to hydrate the query and do the extra filtering in Linq-to-Objects), and
You may want to compare the data of the tags rather than instances (unless you have overridden Equals on Tag
something like:
where movie.Tags.Any(mt => tags.Any(t => t.ID == mt.ID)) // or whatever property(ies) of `Tag` defines equality
You can intersect both list and see if there is any element like:
&& movie.Tags.Intersect(tags).Any()
Since Tag is an object, It will compare the references. Instead you can select unique identifier from Tag and then intersect that like:
&& movie.Tags.Select(r=> r.ID).Intersect(tags.Select(t=> t.ID)).Any()
Where ID is the primary key of Tag table.

Linq2Entities Equivalent Query for Parent/Child Relationship, With All Parents and Children, Filtering/Ordering Children

So the question is ridiculously long, so let's go to the code. What's the linq2entities equivalent of the following Sql, given entities (tables) that look like:
Parent
---
parent_id
parent_field1
Child
--
child_id
parent_id
child_field1
child_field2
The sql:
select p.*, c.*
from parent p
inner join p on
p.parent_id = child.parent_id
where
c.child_field1 = some_appropriate_value
order by
p.parent_field1
c.child_field2
L2E let's you do .include() and that seems like the appropriate place to stick the ordering and filtering for the child, but the include method doesn't accept an expression (why not!?). So, I'm guessing this can't be done right now, because that's what a lot of articles say, but they're old, and I'm wondering if it's possible with EF6.
Also, I don't have access to the context, so I need the lambda-syntax version.
I am looking for a resultant object hierarchy that looks like:
Parent1
|
+-- ChildrenOfParent1
|
Parent2
|
+-- ChildrenOfParent2
and so forth. The list would be end up being an IEnumerable. If one iterated over that list, they could get the .Children property of each parent in that list.
Ideally (and I'm dreaming here, I think), is that the overall size of the result list could be limited. For example, if there are three parents, each with 10 children, for a total of 33 (30 children + 3 parents) entities, I could limit the total list to some arbitrary value, say 13, and in this case that would limit the result set to the first parent, with all its children, and the second parent, with only one of its children (13 total entities). I'm guessing all of this would have to be done manually in code, which is disappointing because it can be done quite easily in SQL.
when you get a query from db using entityframewrok to fetch parents, parent's fields are fetched in single query. now you have a result set like this:
var parentsQuery = db.Parents.ToList();
then, if you have a foreign key on parent, entityframework creates a navigation property on parent to access to corresponding entity (for example Child table).
in this case, when you use this navigation property from parent entities which already have been fetched, to get childs, entityframework creates another connection to sql server per parent.
for example if count of parentsQueryis 15, by following query entityframework creates 15 another connection, and get 15 another query:
var Childs = parentsQuery.SelectMany(u => u.NavigationProperty_Childs).ToList();
in these cases you can use include to prevent extra connections to fetch all childs with its parent, when you are trying to get parents in single query, like this:
var ParentIncludeChildsQuery = db.Parents.Include("Childs").ToList();
then by following Query, entityframework doesn't create any connection and doesn't get any query again :
var Childs = ParentIncludeChildsQuery.SelectMany(u => u.NavigationProperty_Childs).ToList();
but, you can't create any condition and constraint using include, you can check any constraint or conditions after include using Where, Join, Contains and so forth, like this:
var Childs = ParentIncludeChildsQuery.SelectMany(u => u.NavigationProperty_Childs
.Where(t => t.child_field1 = some_appropriate_value)).ToList();
but by this query, all child have been fetched from database before
the better way to acheieve equivalent sql query is :
var query = parent.Join(child,
p => p.ID
c => c.ParentID
(p, c) => new { Parent = p, Child = c })
.Where(u => u.Child.child_field1 == some_appropriate_value)
.OrderBy(u => u.Parent.parent_field1)
.ThenBy(u => u.Child.child_field2)
.ToList();
according to your comment, this is what you want:
var query = parent.Join(child,
p => p.ID,
c => c.ParentID,
(p, c) => new { Parent = p, Child = c })
.Where(u => u.Child.child_field1 == some_appropriate_value)
.GroupBy(u => u.Parent)
.Select(u => new {
Parent = u.Key,
Childs = u.OrderBy(t => t.Child.child_field2).AsEnumerable()
})
.OrderBy(u => u.Parent.parent_field1)
.ToList();

Any way to add a property using linq?

So I have this list, it returns an ID and a thumbnail. ex. List<PersonPicture>
and I have this list, List<Person> which has a property named "picture" in it.
Is there anyway that I can merge this two properties and add the List<PersonPicture> to the property named "picture" in it and base this via the ID since they have the same?
Any help would be appreciated.
You can use an anonymous object for this, below an example:
List<PersonPicture> pictures = LoadPictures();
List<Person> persons = LoadPersons();
var result = persons.Select(pers => new
{
Id = pers.Id,
Name = pers.Name,
Picture = pictures.Where(pic => pic.PersId == pers.Id)
.FirstOrDefault()
.Thumbnail
};
Another solution is to use a Join:
var result = persons.Join(pictures,
pers => pers.Id,
pic => pic.PersId,
(pers, pic) => {
return new
{
Id = pers.Id,
Name = pers.Name,
Picture = pic.Thumbnail
};
});
LINQ isn't quite designed for modifying existing collections like this, but you can do it:
foreach (tup in people
.Join(
picture,
person => person.ID,
picture => picture.ID,
Tuple.Create
))
{
tup.Item1.Picture = tup.Item2;
}
EDIT: Note that this will produce unpredictable results if a person has more than one picture. Is this a possibility, and how should it be dealt with?
You could either use a Join or the Zip operator in linq. These links will take you to questions about the syntax of using both of them. Basically the Join just adds the two lists together based on a key just like in SQL and the Zip merges the two lists by matching the position of each element in each list..
You want to join the two lists based on a shared key -- the ID.
Basically, you want to use the Join operator in LINQ to find pairs of Person and PersonPicture that match the same ID:
persons.Join(pictures, // join these two lists
person => person.Id, // extract key from person
personPicture => personPicture.PersonId, // extract key from picture
(person, personPicture) => ??? // do something with each matching pair
The question you now face is what to do with each matching pair; Join lets you supply a delegate that takes a matching pair and returns something else, and the result of the Join operation will be a list of those 'something else's produced from each of the matching pairs.
Your problem is that you want to take each pair and do something with it -- specifically, you want to copy the picture from the PersonPicture object to the Person object. Since LINQ is all about finding data but not modifying it, this is not trivial.
You can do this in two ways. One is to create a temporary object from each pair, and then iterate over that and do your thing:
var pairs = persons.Join(pictures,
person => person.Id,
personPicture => personPicture.PersonId,
(person, personPicture) => new { person, personPicture };
foreach (var pair in pairs)
pair.person.Picture = pair.personPicture.Thumbnail;
(You can use a Tuple instead of a temporary object, as was suggested in another answer).
This works, but seems clumsy because of the temporary object (be it an anonymous object or a tuple).
Alternatively, you can do the assignment right inside the delegate, and return the Person object itself, since you're done with the PersonPicture object:
var personsWithPicturesPopulated = persons.Join(pictures,
person => person.Id,
personPicture => personPicture.PersonId,
(person, personPicture) => {
person.Picture = personPicture.Thumbnail;
return person;
});
This has the added bonus of giving you the list of persons for which you found a match in the personPictures list, omitting the ones without a match; this is sometimes exactly what you need (and other times it isn't, in which case you can discard the result of the join).

LINQ get parent with most children

I have a list of Parent objects that has a list of Children objects. I need to write a query that would give me the parent that has the most children. The ORM is entity framework, so it should work with that.
Code to start with:
parents.FirstOrDefault(c => c.Children.Max());
Something like that.
I think it should look more like this:
parents.OrderByDescending(p => p.Children.Count()).FirstOrDefault();
Your query is not correct, because c.Children.Max() will try to iterate over children of one parent, and if they support comparison (e.g. children are Ints), will simply return biggest of them. And most probably your Children objects are not bool, so you won't be able to even compile the code, because FirstOrDefault takes
Expression<T, bool>
You don't need sorting for this:
int maxChildCount = parents.Max(x => x.Children.Count());
var maxParent = parents.FirstOrDefault(p => p.Children.Count() == maxChildCount);
Or as query expression:
var maxParent = (from p in parents
let max = parents.Max(x => x.Children.Count())
where p.Children.Count() == max).FirstOrDefault();

Resources