Union Two Linq Queries

本文关键字:Queries Linq Two Union | 更新日期: 2023-09-27 18:22:37

我有两个返回结果集的LinqToSql查询:

var grResults = (from g in ctx.GeneralRequests
                             join rt in ctx.RequestTypes on g.RequestTypeId equals rt.RequestTypeId
                             join sub in ctx.Logins on g.SubmitterStaffId equals sub.LoginId
                             join onb in ctx.Logins on g.OnBehalfOfStaffId equals onb.LoginId
                             where sub.GadId == gadId
                             select new
                             {
                                 Status = "Submitted",
                                 RequestId = g.GeneralRequestId,
                                 Submitter = sub.UserName,
                                 OnBehalf = (onb == null ? string.Empty : onb.UserName),
                                 RequestType = (rt == null ? string.Empty : rt.Description),
                                 ProjectName = (g == null ? string.Empty : g.ProjectName) ,
                                 Comments =  (g == null ? string.Empty :  g.Comments),
                                 LastUpdate = g.LastUpdateDate
                             });
  var grdResults = (from gd in ctx.GeneralRequestDrafts
                                  join rt in ctx.RequestTypes on gd.RequestTypeId equals rt.RequestTypeId
                                  into tempRequestTypes
                                  from rt1 in tempRequestTypes.DefaultIfEmpty()
                                  join onb in ctx.Logins on gd.OnBehalfOfStaffId equals onb.LoginId
                                  into tempOnBehalf
                                  from onb1 in tempOnBehalf.DefaultIfEmpty()
                                  join sub in ctx.Logins on gd.SubmitterStaffId equals sub.LoginId                                  
                                  where sub.GadId == gadId
                                  select new
                                  {
                                      Status = "Draft",
                                      RequestId = gd.GeneralRequestDraftId,
                                      Submitter = sub.UserName,
                                      OnBehalf = (onb1 == null ? string.Empty : onb1.UserName),
                                      RequestType = (rt1 == null ? string.Empty : rt1.Description),
                                      ProjectName = (gd.ProjectName == null ? string.Empty : gd.ProjectName),
                                      Comments = (gd.Comments == null ? string.Empty : gd.Comments),
                                      LastUpdate = gd.LastUpdateDate
                                  });

问题是当我试图把他们联合起来的时候。

 var results = grResults.Union(grdResults).OrderByDescending(r => r.LastUpdate);

即使两个单独的查询都返回,这也不会返回任何记录。

Union Two Linq Queries

由于这两个查询似乎并不相互依赖,所以如果您只是想获得一个列表,只需执行这两个并合并每个查询的结果。

var results = grResults.ToList().Union(grdResults.ToList())
    .OrderByDescending(r => r.LastUpdate);