数据表上的并集

本文关键字:数据表 | 更新日期: 2023-09-27 17:57:50

我将如何在Linq中转换此SQL查询,对不起,我在Linq 方面不是很专业

select ConnectionId
from LearnerConnections
where LearnerId = 1
union
select LearnerId
from LearnerConnections
where ConnectionId = 1

我还可以编写DataTable方法来获得结果吗(比如DataTable.Select()方法)?

提前感谢

数据表上的并集

类似的东西

LearnerConnections.Where(x => x.LearnerId == 1)
                  .Select(m => m.ConnectionId)
                  .Union(LearnerConnections.Where(l => l.ConnectionId ==1)
                                           .Select(lc => lc.LearnerId)
                  );

有了数据表,它应该看起来像

  dtLearnerConnections.AsEnumerable()
                      .Where(m => m.Field<int>("LearnerId") == 1)
                      .Select(m => m.Field<int>("ConnectionId"))
                      .Union(dtLearnerConnections.AsEnumerable()
                                                 .Where(x => x.Field<int>("ConnectionId") == 1)
                                                 .Select(x => x.Field<int>("LearnerId"))
                      );

这将有助于

var results = (from l in LearnerConnections where l.LearnerId == 1 
                 select l.ConnectionId).Union(from a in LearnerConnections 
                 where a.ConnectionId == 1 select l.LeaenerId);
var result = (from lc in LearnerConnections
              where lc.LearnerId == 1
              select lc.ConnectionId)
              .Union
             (from lc in LearnerConnections
              where lc.ConnectionId == 1
              select lc.LearnerId);

如果您使用的是DataTables,则这涉及到LINQ to DataSet的使用。这个SO问题也可能对你有所帮助。