开发者

Using Max in LINQ to SQL

Trouble with using Max in where clause of LINQ to SQL. Data below:

QID,   Question,     TypeID,    Disable, VersionID, Sequence
1      Who's on 1st   1          False    1          1
2      Who's on 1st   1          False    2          1
3      What's on 2nd  1          False    1          2
4      What's on 2nd  1          False    2          2
5      I don't know   1          False    1          3
6      I don't know   1          False    2          3

I need to return a group of questions based on the Max of the VersionID as noted below. The result I expect from the data above would include rows 2, 4 & 6 ordered by Sequence.

IEnumerable&l开发者_StackOverflow社区t;QUESTION> questions = 
  (from q in dataContext.QUESTIONs
   where q.TypeID == Convert.ToInt16(ddlType.SelectedValue)
     && (q.Disable == null || q.bDisable == false)
     && (q.VersionID == dataContext.QUESTIONs.Max(q.nVersionID))
   orderby q.Sequence ascending
   select q);


Max() translates properly in linq-to-sql

Try

IEnumerable<QUESTION> questions = (from q in dataContext.QUESTIONs
                      let maxVersion = dataContext.QUESTIONs.Max(q.nVersionID)
                      where q.TypeID ==     Convert.ToInt16(ddlType.SelectedValue)
                      && (q.Disable == null || q.bDisable == false)
                      && (q.VersionID == maxVersion)
                      orderby q.Sequence ascending
                      select q);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜