question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LINQ Any translated differently from v2

See original GitHub issue

Discussed in https://github.com/linq2db/linq2db/discussions/3429

<div type='discussions-op-text'>

Originally posted by hkarask February 17, 2022 Hi all,

I’m trying to bump the linq2sql version in an older codebase, from 1.10.2 to 2.0.0 and noticed that LINQ Any gets translated a bit differently:

Given a contrived LINQ statement:

var query =
    from task in con.GetTable<Task>()
    from party in con.GetTable<Party>()
        .Where(p => task.AdminPartyId == p.Id ||
                    con.GetTable<PartyAccess>().Any(pa => pa.PartyId == p.Id && pa.Role == Roles.Admin)
        ).DefaultIfEmpty()
...

I’m seeing different SQL between versions 1 and 2

v 1.10.2

SELECT
 	t.Description,
 	p.Name
 FROM
 	Task t
 		LEFT JOIN Party p ON t.AdminPartyId = p.Id OR EXISTS(
 			SELECT
 				*
 			FROM
 				PartyAccess pa
 			WHERE
 				pa.PartyId = p.Id AND pa.Role = 'Admin'
 		)

v 2.0.0

SELECT
 	j.Description,
 	t2.Name
 FROM
 	Task t
 		LEFT JOIN (
 			SELECT
 				p.Name,
 				p.Id
 			FROM
 				Party p
 			WHERE
 				(EXISTS(
 					SELECT
 						*
 					FROM
 						PartyAccess pa
 					WHERE
 						pa.PartyId = p.Id AND pa.Role = 'Admin'
 				))
 		) t2 ON t.AdminPartyId = t2.Id

There’s now a full sub-query instead of join a level filter. This is causing a full table scan for some reason (using MySql 5.6). Is there a way to revert back to the old behaviour?

Environment details

linq2db version: 1.10.2 & 2.0.0 Database Server: MySql 5.6 Database Provider: MySql.Data 8.0.25 .NET Framework: .NET 4.8

Thanks!

</div>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Feb 22, 2022

@sdanyliv planned to look at it. It depends on complexity of required fix - right now we want to concentrate on v4 preparation, so if fix will require a lot of work - it will be postponed till v4, taking into account that there is a workaround…

0reactions
hkaraskcommented, Feb 22, 2022

Do you think it’s possible to go back to the v1 join behaviour in an upcoming release, at least for MySql?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query with nested Any() translation error
I'm trying to write a semi-advanced LINQ to SQL query to search through my entities in a .NET 6 project. The filtering in...
Read more >
How to debug EF.Core LINQ translation issues? : r/dotnet
Start with non-working LINQ expression and gradually strip it, removing sub-properties one by one, until it works. If you end up with root-level ......
Read more >
Writing Better Performing Queries with LINQ on EF Core 6.0 ⚙️
First of all, what is LINQ? ... These two queries will translate to same SQL if you keep an eye out using SQL...
Read more >
Invalidoperationexception: the LINQ expression
You are misusing the property and EF cannot translate that. In order to use an expression in LINQ it must be translatable to...
Read more >
7 tricks to simplify your programs with LINQ - Igor Ostrovsky
1. Initialize an array · 2. Iterate over multiple arrays in a single loop · 3. Generate a random sequence · 4. Generate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found