开发者

Need Linq translation for the following SQL Query

开发者 https://www.devze.com 2022-12-09 07:58 出处:网络
selectcolId, colTaskType, MaxID fromtblTaskType join( select tblCheckList.c开发者_StackOverflow社区olTaskTypeID,
select  colId,
        colTaskType,
        MaxID
from    tblTaskType
join    (
         select tblCheckList.c开发者_StackOverflow社区olTaskTypeID,
                max(colItemNumber) MaxID
         from   tblCheckList
         group by colTaskTypeID
        ) x on coltaskTypeID = tblTaskType.colID


Assuming you are using linq-to-sql and have the two tables in a datacontext.

The more or less exact translation would be:

var maxChecks = from checks in DataContext.tblChecklist
                group checks by checks.colTaskTypeID into g
                select new { colTaskTypeID, max = g.Group.Max(x => x.colItemNumber) };

var result = from t in DataContext.tblTaskType
             join c in maxChecks on t.colTaskTypeID equals c.colTaskTypeID
             select new { t.colId, t.colTaskTypeID, c.max };

But you could try:

var result = from t in DataContext.tblTaskType
             select new {
                     t.colId,
                     t.colTaskTypeID,
                     Max = (from c in DataContext.tblChecklist
                            where c.colTaskTypeID == t.colTaskTypeID
                            select c.colItemNumber).Max() };
0

精彩评论

暂无评论...
验证码 换一张
取 消