C#Linq从多个表中选择数据

时间:2017-02-10 10:12:42

标签: c# sql-server linq

在1个查询中,我想选择2个表(Union)的结果。

表1: StationStatus

StationStatus Table

表2: DetectorStatus

DetectorStatus Table

我可以执行以下查询:

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

这将导致2个表合并为1。

但是,我不知道如何在代码中执行此操作 我当前正在执行的查询仅适用于 DetectorStatus表

    [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;
}

我称之为:

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.
}

我的问题,如何在多个表格中使用它? 因为在查询中我需要调用 StationStatu DetectorStatu ,但我不知道如何执行此操作。

当我向任何内容添加.Union时,我收到以下错误:

Error Message

我正在使用的代码:

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;

1 个答案:

答案 0 :(得分:0)

我无法在单个查询中查看来自两个表的数据的任何理由,而无需以任何方式链接结果。因此,我建议您分别查询表,将它们选择为公共数据对象并将它们合并到客户端而不是服务器端(如果需要,您仍然可以在服务器上对查询进行排序,然后只在客户端合并已排序的数据)。

public class StatusTransferObject
{
    public int DetectorId { get; set; }
    public DateTimeOffset DateTime { get; set; }
    public int? DetectorModeId { get; set; }
    public int? StationModeId { get; set; }
    public decimal Status { get; set; }
}

var orderedObjects1 = ObjectContext.DetectorStatus.OrderBy(s => s.Status).Select(s => new StatusTransferObject
{
    DetectorId = s.DetectorID,
    DateTime = s.DateTime,
    DetectorModeId = s.DetectorModeID,
    StationModeId = null,
    Status = s.Status
});
var orderedObjects2 = ObjectContext.StationStatus.OrderBy(s => s.Status).Select(s => new StatusTransferObject
{
    DetectorId = s.DetectorID,
    DateTime = s.DateTime,
    DetectorModeId = null,
    StationModeId = s.StationModeID,
    Status = s.Status
});
var mergedObjects = MergeSorted(orderedObjects1, orderedObjects2, (x1, x2) => x1.Status.CompareTo(x2.Status)).ToList();

MergeSorted取自以下问题:Is there an easy way to merge two ordered sequences using LINQ?