In 1 query, I want to select results of 2 tables (Union).
Table 1: StationStatus
Table 2: DetectorStatus
I can excecute a query as followed:
SELECT TOP 1000 [DetectorStatusID],
[DetectorID],
[DateTime],
[DetectorModeID],
NULL as StationModeID,
[Status]
FROM [GMS_MAN].[dbo].[DetectorStatus]
UNION SELECT TOP 1000 [StationStatusID],
[DetectorID],
[DateTime],
NULL as DetectorMOdeID,
[StationModeID],
[Status] FROM [GMS_MAN].[dbo].StationStatus
ORDER BY Status
Which will result in the 2 tables combined in 1.
However, I don't know how to do this in the code.
The query I'm currently executing is only for the DetectorStatus Table
[Query]
[OutputCache(OutputCacheLocation.Server, 60)]
public IQueryable<DetectorStatu> GetDatabacklog(int userID)
{
User user = ObjectContext.Users
.Include("TeamMemberships")
.First(u => u.UserID == userID);
var teamIDs = user.TeamMemberships.Select(t => t.TeamID).ToList();
var siteIDs = ObjectContext.SiteMemberships.Where(t => teamIDs.Contains(t.TeamID)).Select(t => t.SiteID).ToList();
var DetectorStatus = ObjectContext.DetectorStatus
.Include("Detector")
.Include("Detector.Track")
.Include("Detector.Site")
.Include("Detector.EnabledDetectorTypes")
.Include("Detector.EnabledDetectorTypes.DetectorType")
.Where(d => siteIDs.Contains(d.Detector.SiteID))
.OrderBy(d => d.Status);
return DetectorStatus;
}
And I call it like:
private void GetDatabacklog()
{
LoadOperation<DetectorStatu> loadOp = context.Load(context.GetDatabacklogQuery(App.CurrentUser.UserID), LoadBehavior.RefreshCurrent, LoadDatabacklogCompleted, null);
}
private void LoadDatabacklogCompleted(LoadOperation<DetectorStatu> loadOperation)
{
//The rest of the code where i show the results.
}
My question, how can I use this in multiple tables?
Because in the query I need to call StationStatu and DetectorStatu, however I don't know how to do this.
When I add .Union
to anything, I get the following error:
The code I'm using:
var detectorSt = ObjectContext.DetectorStatus.Select(x => new { x.DetectorID, x.Status});
var stationSt = ObjectContext.StationStatus.Select(x => new { x.DetectorID, x.Status});
var unioned = detectorSt.Union(stationSt);
return unioned;