Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL to LINQ conversion

I have the following SQL statement

SELECT 
    c.CorpSystemID, c.SystemName  , 
    case when a.TaskItemID is NULL then 'false' else 'true' end as Assigned
FROM CorpSystems c 
LEFT OUTER JOIN
     (SELECT CorpSystemID, TASKItemID 
      FROM AffectedSystems 
      where TASKItemID = 1) a ON c.CorpSystemID = a.CorpSystemID

Can anyone please help me to convert this statement to LINQ?

Thank you.

like image 727
Yuri Avatar asked Aug 11 '26 09:08

Yuri


2 Answers

Ok so assume you've got a list of your CorpSystem objects in a variable called Corpsystems and a list of your AffectedSystem objects in a variable called AffectedSystems. Try the following:

Edit: For a join on all Affected Systems, try this:

var matches = from c in CorpSystems
              join a in AffectedSystems on c.CorpSystemId equals a.CorpSystemId into ac
              from subSystem in ac.DefaultIfEmpty()
              select new
                     {
                         c.CorpSystemId,
                         c.SystemName,
                         Assigned = subSystem != null && subSystem.TaskItemId != null
                     };

Or for just AffectedSystems that have a TaskItemId of 1:

var matches = from c in CorpSystems
              join a in AffectedSystems.Where(as => as.TaskItemId == 1)
                  on c.CorpSystemId equals a.CorpSystemId into ac
              from subSystem in ac.DefaultIfEmpty()
              select new
                     {
                         c.CorpSystemId,
                         c.SystemName,
                         Assigned = subSystem != null && subSystem.TaskItemId != null
                     };
like image 143
mattytommo Avatar answered Aug 12 '26 22:08

mattytommo


See the answers to the following SO question SQL to LINQ Tool, assuming that you do not want to go through the process by hand.

like image 44
Joshua Drake Avatar answered Aug 12 '26 23:08

Joshua Drake



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!