Xour
Xour
CC#
Created by Xour on 8/4/2024 in #help
Issues with LINQ query to left join three tables
Hi everyone, I’m running into some issues with a LINQ query. I need to join three tables, but I’m having trouble getting the query to work as expected. The constraints are: - No navigation properties between the tables - Two tables (RobotNetworkStatus and RobotSystemStatus) contains either zero or one row for a given RobotSerialNumber - RobotNetworkInterfaces contains zero or more rows for any given RobotSerialNumber After poking at the docs (here and here), this is what I come up with, which is of course not working (cannot be evaluated property, and an exception is thrown):
var robotNetworkData = await (
from ni in _dbContext.RobotNetworkInterfaces
join ns in _dbContext.RobotNetworkStatus
on ni.RobotSerialNumber equals ns.RobotSerialNumber into nsGroup
from ns in nsGroup.DefaultIfEmpty()
join ss in _dbContext.RobotSystemStatus
on ni.RobotSerialNumber equals ss.RobotSerialNumber into ssGroup
from ss in ssGroup.DefaultIfEmpty()
where ni.RobotSerialNumber == request.SerialNumber
group new { ni, nsGroup, ssGroup } by new { ni, ns, ss } into g
select new
{
RobotNetworkStatus = g.Key.ns,
RobotSystemStatus = g.Key.ss,
RobotNetworkInterfaces = g.Select(nd => nd.ni).ToList()
}
)
.AsNoTracking()
.ToListAsync();
var robotNetworkData = await (
from ni in _dbContext.RobotNetworkInterfaces
join ns in _dbContext.RobotNetworkStatus
on ni.RobotSerialNumber equals ns.RobotSerialNumber into nsGroup
from ns in nsGroup.DefaultIfEmpty()
join ss in _dbContext.RobotSystemStatus
on ni.RobotSerialNumber equals ss.RobotSerialNumber into ssGroup
from ss in ssGroup.DefaultIfEmpty()
where ni.RobotSerialNumber == request.SerialNumber
group new { ni, nsGroup, ssGroup } by new { ni, ns, ss } into g
select new
{
RobotNetworkStatus = g.Key.ns,
RobotSystemStatus = g.Key.ss,
RobotNetworkInterfaces = g.Select(nd => nd.ni).ToList()
}
)
.AsNoTracking()
.ToListAsync();
I suspect the issue relies in the grouping. NGL, I was more or less guessing what to do here. Ideally I would like to get this fixed, but more importantly, I would love to understand how to fix it and how does it work. Any hit/help/advise/suggestions are most welcomed! Thanks! EDIT: If I remove the grouping it works, but for each row of RobotNetworkInterfaces I get all other rows as well.
8 replies
CC#
Created by Xour on 5/6/2024 in #help
Options pattern in a shared class library
Hello all, I am looking to use options pattern in a shared class library, but I am not sure how should I go about it. The project structure is something like this:
- Api_1
- Layer_1
- Layer_2
..
- Api_2
- Layer_1
- Layer_2
..
- Shared class libraries
- Class_library_1
- Class_library_2
..
- Api_1
- Layer_1
- Layer_2
..
- Api_2
- Layer_1
- Layer_2
..
- Shared class libraries
- Class_library_1
- Class_library_2
..
What I need is to somewhat use options pattern in, say, Class_library_2 (or at least have access to the configuration provider in any way), but I am unsure what would be the proper way to do this. If I register the config in the Startup.cs of the Api_1 web project, those won't be available from Api_2. Should I create the option classes and register the config in both projects? Any hints or advice? Thanks a lot!
29 replies