Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Union组合两个linq查询_C#_Linq - Fatal编程技术网

C# 如何使用Union组合两个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

我有两个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() 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(
)。