2012-11-08 227 views
1

如何將其轉換爲linq? Thaksselect(select語句)

SELECT AccNo, AccName FROM ( 
SELECT fileNo AS AccNo, fileName AS AccName, 
(SELECT oldID FROM USER WHERE USER.id = ACCOUNT.id) AS oldID, 
(SELECT newID FROM USER WHERE USER.id = ACCOUNT.id) AS newID FROM FILE inner join ACCOUNT ON AccNo= FileAccNo) TblAllTable 
WHERE AccNo="abc" 
GROUP BY AccNo, AccName 
+0

您可以使用此[SQL到LINQ轉換器(http://www.sqltolinq.com/)? – Pezzzz

回答

0

這裏:

var TblAllTable = FILE 
    .Join(ACCOUNT, f => f.accNo, a => a.fileNo, (f, a) => new 
    { 
     AccNo = a.fileNo, 
     AccName = f.fileName, 
     oldID = USER.Where(u => u.ID.Equals(a.id)).First().oldID, 
     newID = USER.Where(u => u.ID.Equals(a.id)).First().newID 
    }); 

var result = TblAllTable 
    .Where(a => a.AccNo.Equals("abc")) 
    .GroupBy(a => new { 
     AccNo = a.AccNo, 
     AccName = a.AccName 
    }) 
    .Select(a => new 
    { 
     AccNo = a.Key.AccNo, 
     AccName = a.Key.AccName 
    }); 

// If you're just selecting grouped keys, 
// it would be better to use Distinct method instead of GroupBy