0

How I will convert this SQL query in Linq, sorry but I am not that much expert in LINQ

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

also can I write the DataTable methode to get the result (like DataTable.Select() method)?

thanks in advance

gofor.net
  • 4,218
  • 10
  • 43
  • 65

3 Answers3

2

something like that

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

                  );

with a datatable, it should look like

  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"))
                      );
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
0

May this will helpful

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);
Gopesh Sharma
  • 6,730
  • 4
  • 25
  • 35
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);

If you are using DataTables, this involves the use of LINQ to DataSet. This SO question might also help you.

Community
  • 1
  • 1
Larry
  • 17,605
  • 9
  • 77
  • 106