简体   繁体   English

使用具有聚合功能的LINQ在列上进行SELECT

[英]SELECT on column using LINQ with an aggregate function

I'm tring to create a dataset that is only the StatusID from the tbBreadCrumb table after the row that is the max BreadCrumbID is selected but not sure how to do it. 我正在尝试创建一个数据集,该数据集仅在选择了最大BreadCrumbID的行之后才能从tbBreadCrumb表中获取,但不知道如何做。 Basically, I only want to return StatusID to JSON but I'm getting red underline saying: "int does not contain a definition set for 'Select' and no extension method 'Select' could you be missing an assembly?" 基本上,我只想将StatusID返回到JSON,但是我得到的红色下划线是:“ int不包含'Select'的定义集,并且没有扩展方法'Select'会丢失程序集吗?”

Here's my code: 这是我的代码:

[WebMethod]
    public static object getMaxBreadCrumbByProjectID(int id)
    {
        using (dbPSREntities5 myEntities = new dbPSREntities5())
        {
            var thisProject = myEntities.tbBreadCrumbs.Where(x => x.ProjectID == id).Max(x => x.BreadCrumbID);

            var columns = thisProject.Select(x => new { x.StatusID }).ToList(); <--- this has the red underline and error message.

            return columns;
        }
    }

As the error is trying to tell you, thisProject is already a number. 由于错误试图告诉您, thisProject已经是一个数字。

Max() returns the highest value, not a collection. Max()返回最大值,而不是集合。

Max returning your Maximum BreadCrumbId which is an integer.Try this: Max返回您的Maximum BreadCrumbId ,它是一个整数。请BreadCrumbId以下操作:

var id = myEntities.tbBreadCrumbs.Where(x => x.ProjectID == id).Max(x => x.BreadCrumbID);
var columns = myEntities.tbBreadCrumbs.Where(x => x.BreadCrumbID == id)
                                      .Select(x => x.StatusID).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM