选择(选择陈述)

时间:2012-11-08 05:25:05

标签: vb.net linq linq-to-sql linq-to-entities

如何将其转换为linq? Thaks

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

1 个答案:

答案 0 :(得分: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