score:0
I'd use a Select
to get the entities with OrderBy after the Where
but before FirstOrDefaultAsync()
. Like this:
var header = await _context.Headers
.Include(location => location.HeaderLocation)
.Include(details => details.Details)
.Where(p => p.HeaderId == HeaderId)
.Select(header => new Header
{
// Assign Header values
Location = header.Location,
Details = header.Details.OrderBy(h => h.FieldName).OrderBy(h => h.LineVersion)
}).FirstOrDefaultAsync();
score:2
On top of the other suggestion I have seen, I used the link in the comments of the original post, and got a great answer there, too. I tested it, and it works like a charm. Here is what I ended up with:
public async Task<PermitHeader> GetPermit(int HeaderId)
{
var header = await _context.Headers
.Include(location => location.Location)
.Where(p => p.HeaderId == HeaderId).FirstOrDefaultAsync();
var details = await _context.Details
.OrderBy(ob => ob.FieldName)
.OrderBy(ob => ob.LineVersion)
.Where(d => d.HeaderHeaderId == HeaderId).ToListAsync();
header.Details = Details;
return header;
}
Thanks for a quick response!
Source: stackoverflow.com
Related Articles
- Lambda expression used inside Include is not valid
- Lambda expression used inside Include is not valid. EF6, Navigation Property
- EF Core Filtered Include: "Lambda expression used inside Include is not valid"
- C# multiple variables in lambda expression inside LinQ query
- Include using Lambda expression
- 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities' when lambda is passed as a parameter, but not when used directly
- Where condition inside lambda expression c#
- linq to entity - include with lambda expression
- Dynamic Lambda Expression inside an Expression Query
- Lambda expression to grab all values inside a Dictionary
- The Include property lambda expression [...] is invalid. The expression should represent a property access
- Lambda Expression for Many to Many realtionship in C# EF 5 Code First
- Can a LINQ Expression defined as a lambda expression include other LINQ Expressions?
- using where clause inside include generate error "The Include path expression must refer to a navigation property defined on the type"
- Looping though collection inside other collection and LINQ lambda expression
- How to find the index of an element in an array inside a lambda expression in c#
- Using String.compare inside a lambda expression
- AddRange/concat functionality inside a lambda Select expression
- How to write the same code using Lambda Expression
- How to write following code in lambda expression or linq?
- LINQ, Use Lambda Expression in Include Function
- ForEach loop with Lambda expression in Razor code MVC 5 For List<T>
- VS Code Coverage won't recognize only possible Expression Lambda Path
- C# LINQ to SQL Specified type member inside lambda expression
- How to restructure Linq query to avoid using Contains() inside an Any() lambda expression (need a ContainsAll())
- Get values from one list inside one list using lambda expression
- Lambda expression with statement body error in previously working code
- How to capture type of a local variable inside a lambda expression in linq
- Condition inside a Lambda Expression
- How to make a linq lambda expression and set the table to be used in the expression dynamically?
- Linq 'equals' keyword Revisited - Does it compare values and references to objects?
- LINQ GroupBy not grouping results
- Optimize Linq code
- How do I to use Where, Group By, Select and OrderBy at same query linq?
- ICollection<T> calculate average per hour using LINQ
- How to run single process for multiple values in c#?
- Sort groups based on values within groups
- How to break or Exit from Linq query
- Ensure IQueryable<T>.Where() runs a SQL query rather than filtering in memory
- C# Check If List Contains Similiar Strings Using LINQ
- Linq query is not working using another query inside
- Using LINQ to change values in collection
- .SelectMany() and getting data from more than one related table
- GroupBy and average a column in Linq
- How to populate a complex object using Linq to Sql
- Linq record not in other table
- Group and join give missing field
- Only parameterless constructors and initializers are supported in LINQ to Entities error while doing version check
- Refactor two linq queries into one single query
- linq query with dynamic where clause