Wrong subquery
See original GitHub issueI’m using Linq2Db v5.2.2 in a .NET7.0 project. I have this code
await using var db = new Database.Database();
var pcScanHdd = GetPcScanId(db, ScanFlags.HardDisks);
//var a = await pcScanHdd.ToListAsync();
var query =
from ps in pcScanHdd
join pc in db.Pcs on ps.PcId equals pc.Id
join scan in db.Scan on ps.ScanId equals scan.Id
join disk in db.LogicalHdds on scan.Id equals disk.ParentId
where disk.MediaType == MediaTypeEnum.FixedHardDisk
&& disk.DriveType == DriveTypeEnum.LocalDisk
select new { pc, scan, disk };
GetPcScanId
is a function returning an IQueryable<PcScanId>
where PcScanId is defined as
private record PcScanId
{
public int PcId;
public int ScanId;
}
var pcScanHdd = GetPcScanId(db, ScanFlags.HardDisks)
returns this query
SELECT
pc.id,
Max(scan_1.id)
FROM
scan scan_1
INNER JOIN pcs pc ON scan_1.id_parent = pc.id
WHERE
pc.endDate IS NULL AND @flags = (scan_1.flags & @flags_1)
GROUP BY
pc.id
If I try var a = await pcScanHdd.ToListAsync();
I get correct result.
Unfortunately when I use pcScanHdd in the next query, subquery is completely wrong:
FROM
(
SELECT
0 as PcId
FROM
scan scan_1
INNER JOIN pcs pc ON scan_1.id_parent = pc.id
WHERE
pc.endDate IS NULL AND @flags = (scan_1.flags & @flags_1)
GROUP BY
pc.id
) ps
INNER JOIN pcs pc_1 ON ps.PcId = pc_1.id
INNER JOIN scan scan_2 ON ps.PcId = scan_2.id
INNER JOIN info_logicaldisk disk ON scan_2.id = disk.id_parent
WHERE
disk.MediaType = 12 AND disk.DriveType = 3
SELECT 0 as PcId
is bad, but INNER JOIN scan scan_2 ON ps.PcId = scan_2.id
is a nonsense.
I can’t understand why it works if I execute the query itself, but it goes bad when I use it in another query.
Issue Analytics
- State:
- Created 3 months ago
- Comments:14 (7 by maintainers)
Top Results From Across the Web
Error when running query as a subquery [closed]
Error when running query as a subquery [closed] select as as fr. rating , sp. SpId ,sp. FName ,sp. LName ,sp. DpUrl ,sp....
Read more >What is wrong with this subquery?
1 Answer 1 ... When you use an aggregation function without GROUP BY , it aggregates the entire table into a single result...
Read more >Access subquery returns the wrong values
Access subquery returns the wrong values. Hi All,. I have a table in Access 2007 which has records for working days. I used...
Read more >Solved: SQL subquery, wrong variable name, no error
SQL subquery, wrong variable name, no error ... The issue is that RECORDID is not is DELTA.DATADIFF as illustrated by the second two...
Read more >Subquery returned more that 1 value. This is not permitted...
When executing a SQL query or a SQL job, it fails with the following error message: Subquery returned more than 1 value. This...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well it is doable in different way:
Seems like with your updated query it’s working. Let me test it some more… and thank you 😃