C# 如何使用Union组合两个linq查询
我有两个Linq查询要C# 如何使用Union组合两个linq查询,c#,linq,C#,Linq,我有两个Linq查询要Merge我想使用Union,我该怎么做 问题1 var result = (from pni in _entities.PushNotificationInfoes join ssg in _entities.StudentStaffGuardians on pni.UniqueID equals ssg.StaffId join st in _entities.Students on ssg.StudentId.GetValueOrDefault
Merge
我想使用Union
,我该怎么做
问题1
var result = (from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentStaffGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
}).ToList()
问题2
var result = (from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
}).ToList()
直接怎么做,从查询一个像}).ToList().Union
var result1 = (from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentStaffGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
});
var result2 = (from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
});
var result = result1.Union(result2).ToList();
无需在result1
和result2
上使用ToList
。通过这种方式,union可以在持久层中解析(如果这是EF)
无需在
result1
和result2
上使用ToList
。通过这种方式,union可以在持久层中解析(如果这是EF)。代码应该如下所示:
var result = (from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentStaffGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
})
.Union(
from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
}).ToList();
根据你的评论,我猜你不明白查询是如何执行的。
.ToList()
之前的所有查询文本都只是查询组合。当您感觉已经完成查询并准备好执行它时,应该调用.ToList()
代码如下所示:
var result = (from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentStaffGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
})
.Union(
from pni in _entities.PushNotificationInfoes
join ssg in _entities.StudentGuardians on pni.UniqueID equals ssg.StaffId
join st in _entities.Students on ssg.StudentId.GetValueOrDefault() equals st.StudentID
where st.OrganizationID == orgId
select new
{
pni.UserID,
pni.PNToken,
pni.OSType,
}).ToList();
根据你的评论,我猜你不明白查询是如何执行的。
.ToList()
之前的所有查询文本都只是查询组合。当您感觉已经完成查询并准备好执行它时,您应该调用.ToList()
这两个查询之间的区别是什么?有没有ssg表这两个查询之间的区别是什么?有没有ssg表您可以告诉我如何做})。ToList().Union
?正如我在问题中提到的。谢谢你@CGPA6.4-只需在末尾添加ToList
,就像我从result1
和result2
中删除它之前一样。你能告诉我怎么做})。ToList().Union
?正如我在问题中提到的。谢谢你@CGPA6.4-只需在末尾添加ToList
,就像我从result1
和result2
中删除它之前一样。你不是在之前写。ToList
。Union
不需要在之前写。ToList
最后一个应该是)。ToList();代码>或此}.ToList())代码>应该与我的答案相同<代码>}),ToList()代码>供参考:我们可以编写.Union(
如果.Union(
你不是在之前写.ToList
,Union
?不需要在之前写.ToList
。Union
最后一个应该是这个])。ToList();
或者这个.ToList()代码>应该与我的答案相同<代码>}),ToList()
FYI:如果.Union(
),我们也可以编写.Union(
)。