score:1
Yes unfortunately you can't use Include
extension method that way, but if you are open to use a third party library then I recommend you to use Entity Framework Plus, with that lib you could do this:
var persons = _context.Persons
.IncludeFilter(e => e.Items.OrderByDescending(i => i.Date).Take(1))
.ToList();
There is a second option could be using Global Filters but I think first solution is close to what you are looking for.
A third option would be to project the query with the result you are expecting:
var persons = _context.Persons
.Select(e=> new {Person=e,
Item=e.Items.OrderByDescending(i => i.Date)
.Take(1)
})
.ToList();
score:1
Yes, unfortunately are missusing Include
.
For your queries, you could use a GroupBy
on Items
and a subquery, though.
Query on the items, grouped by PersonId, ordered by date inside the group, and take first of them.
Hopefully you'll have some relevant index in place to speed up the query on the db side.
That should be something like :
_context.Items.GroupBy(i => i.PersonId)
.Select(g => g.OrderByDescending(p => p.Date).FirstOrDefault())
Source: stackoverflow.com
Related Query
- LINQ filter on navigation property
- Linq to Entity Filter using Navigation Property
- How to filter navigation property of a list by LINQ and Lambda expression?
- LINQ to Entities - filter on property of navigation property
- Filter all the record having navigation property in Linq .Net MVC
- Filter linq list on property value
- EntityFramework 5 filter an included navigation property
- LINQ complex query navigation property
- Filter two lists on one property c# using linq
- Use Linq to filter Hashtable based on a property of Values custom object
- Dynamic LINQ Expression for sorting navigation property
- LINQ Source Code Available
- Combine two linq expressions to inject navigation property
- How to use LINQ to filter property of child collection using (.Any())
- Entity Framework How can I filter my results by a property of a navigation property?
- LINQ filter list based on property value and filter the properties as well
- How to filter a list with linq depends on counting a property at the same list and take a random group at least minimum total
- Need Help Filtering A LINQ Query Using An Entity Framework Navigation Property
- creating Linq to sqlite dbml from DbLinq source code
- C# LINQ filter results based on property filed exist and value match
- How do i use an Expression<Func<T>> in a linq statement if my navigation property is a single item?
- LINQ query to filter list by object property with multiple checks
- Can I load hierarchical data with Entity Framework 4 using ".Include" and filter a Navigation Property based on some criteria
- Dynamic Where filter through navigation Property
- Filter query who contains ICollection property via LINQ
- Use linq to filter by property of subcollection
- Filter navigation property EF core
- C# Linq Filter IEnumerable dynamic property and value
- Linq to Entities filter navigation collection properties
- LINQ to entity query, check null before using navigation property
More Query from same tag
- Nested Query or Sub query linq
- EF Core query only DateTime of DateTimeOffset cannot be translated;
- use Expression<Func<T,X>> in Linq contains extension
- Calling stored procedure from C# is not working
- C# Linq Lambda for group by multiple columns select max
- Linq error generic parameter or the query must use a nullable type
- Assign a list of strings to a propertis - filtering periods and displaying the number of orders in a given period
- C#.NET LinQ remove Dupes from list with a twist
- To convert if condition to linq in cshtml
- How to select top count, sum
- Angular2 linq style property accessor
- Better way to update/insert values using EF
- LINQ to SQL: Multiple / Single .dbml per project?
- Convert a parent-child-grandchild hierarchy to another p-c-gc hierarchy
- Delegate in a where clause
- Linq "Object reference not set to an instance of an object."
- How to do an "in" query in entity framework?
- LINQ Query on Datatable to check if record exists
- Receiving 409 Error when uploading data to Windows Azure
- MongoDB C# driver fast on take(1) but slow on take(2)
- Convert linq result to name value pair
- How to execute "Parallel.ForEach" as background task that will return control immediate to the calling method?
- Providing a generic key comparison based on a collection of a generic type
- LINQ GroupBy 3 properties?
- Find a value between a List's upper and lower limits
- How to get distinct values in string array via Linq?
- Get Range Group Result from LINQ
- Inner Joining Tables with 2 different variables
- Linq's Column In (Select ...) Statement
- Sequence contains no elements with LINQ FirstOrDefault