在 LINQ to SQL 中使用 Max

发布于 2024-12-05 12:54:02 字数 886 浏览 0 评论 0原文

在 LINQ to SQL 的 where 子句中使用 Max 时出现问题。数据如下:

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

我需要根据 VersionID 的最大值返回一组问题,如下所示。我期望从上面的数据得到的结果将包括第 2、4 和 4 行。 6 按顺序排序。

IEnumerable<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);

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<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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2024-12-12 12:54:02

Max() 在 linq-to-sql 中正确翻译

尝试

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文