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.

I’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:open
  • Created 3 months ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Jun 28, 2023

Well it is doable in different way:

var query = 
	from pc in db.Pcs
	from scan in pc.Scans
		.Where(scan => pc.DismissedDate == null && (scan.Flags & flags) == flags)
		.OrderByDescending(scan => scan.scanId)
		.Take(1)
	select new 
	{
		pc,
		lastScan = scan
	};
0reactions
mark74commented, Jun 30, 2023

Seems like with your updated query it’s working. Let me test it some more… and thank you 😃

Read more comments on GitHub >

github_iconTop 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 >

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