I'm using Linq2Db from a long time and I've never have to face a mistake in the queries.
I've a function returning an IQueryable
and then another method where I compose another query using the first one as a base.
await using var db = new Database.Database();
var pcScanHdd = GetPcScanId(db, ScanFlags.HardDisks);
//var a = await pcScanHdd.ToListAsync(); // <- Just a check: it works as expected
var query =
from ps in pcScanHdd
join pc in db.Pcs on ps.PcId equals pc.Id
//ERROR: linq2db creates a wrong INNER JOIN in the next line
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 };
In this query take a look at
join scan in db.Scan on ps.ScanId equals scan.Id
that, unfortunately, return this query
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`
I couldn't understand why my query was returning no items, then I realized the reason watching at SQL.
It should be INNER JOIN scan scan_2 ON ps.ScanId = scan_2.id
.
I've tried using .Where()
in linq2sql, but it's processed exactly as the other.
Is there something I'm doing wrong?
If yes, what?
If no, is there a solution?
EDIT
Object returned from the first query has not Linq2Db attributes because it's not persisted in db, so I fill in with a Linq2Sql query:
private record PcScanId
{
public int PcId;
public int ScanId;
}
It works as expected and if I try to check the result using
var a = await pcScanHdd.ToListAsync();
I get correct fields and values.
If I don't use this sub-query but only db objects instead, everything works fine.