CROSS APPLY within LINQ
Is there a tool I can use to directly map SQL to LINQ? I have a query that I'm not sure how to translate. Here's the SQL:
I can't figure out how to do
CROSS APPLY
within LINQ.
This was my attempt, but it creates a horrible web of SQL that traverses the entire Assignments
table just to find a single record.
The only way we've found that works so far is to not add the LatestFile
here, and run a client-side foreach
loop after we get the results.5 Replies
More than 2hrs and no answer, I'm by far no expert but I suggest using LINQ with query-syntax for that purpose. It may clarify things.
Also
CROSS APPLY
is afaik just a multiple SELECT
in LINQ, so it might look like this
I have seen that Stack Overflow reply. Is there a way with method chaining?
There is, through it's not needed. No matter which syntax you use, either query-syntax or the extenstion method (fluent style).
Under the hood it's still the same code.
You might wanna take a look at this:
https://codeblog.jonskeet.uk/2011/01/28/reimplementing-linq-to-objects-part-41-how-query-expressions-work/
jonskeet
Jon Skeet's coding blog
Reimplementing LINQ to Objects: Part 41 – How query expressions work
Okay, first a quick plug. This won’t be in as much detail as chapter 11 of C# in Depth. If you want more, buy a copy. (Until Feb 1st, there’s 43% off it if you buy it from Manning with …
I know it's the same code. It's that everywhere in the codebase uses method chaining, and we would like to keep it that way. I've solved it with method chaining by having a separate foreach loop after it's been enumerated, but there should be a way to do it properly. This seems like giving up. I would like to find a tool that can show the SQL above, as LINQ. Similar to how you can change between C#/VB/IL with dnSpy. LIke LinqPad, but in reverse.
don't think there is such a tool. But you can always work backwards if you need to, sometimes produces a cleaner SQL query in these sorts of cases where EF otherwise struggles to do so
ctx.AssignmentFiles.OrderByDescending(...).Where(x => x.Assignment.ClientName == command.ClientName).Select(x => new AssignmentDetailsViewModel { FileName = x.Assignment.AssignmentDetails.FileName, ... }