联盟数据表

时间:2013-02-05 13:02:09

标签: c# asp.net

我将如何在Linq中转换此SQL查询,对不起,但我在LINQ中并不是那么多专家

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

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

提前致谢

3 个答案:

答案 0 :(得分:2)

类似的东西

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

答案 1 :(得分:0)

愿这会有用

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

答案 2 :(得分:0)

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 question也可以帮助你。