0

我将如何在 Linq 中转换这个 SQL 查询,对不起,但我不是 LINQ 方面的专家

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

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

提前致谢

4

3 回答 3

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"))
                      );
于 2013-02-05T13:07:24.790 回答
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);
于 2013-02-05T13:07:52.917 回答
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也可能对您有所帮助。

于 2013-02-05T13:09:11.933 回答