Union Two Linq Queries

你。 提交于 2020-12-31 14:56:25

问题


I have two LinqToSql queries that return result sets:

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

The problem is when I try to Union them.

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

This returns no records even though the two individual queries do.


回答1:


Since the 2 queries don't appear to rely on each other just execute both and union the results of each if you are just trying to get a single list.

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


来源:https://stackoverflow.com/questions/33811510/union-two-linq-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!