MAX query using CAML

前端 未结 5 681
滥情空心
滥情空心 2021-01-05 05:17

I want to select a sharepoint list item which has the Maximum value for a particular column. How can I do this using CAML queries?

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-05 05:46

    The following CAML query would return the maximum value for a given column:

    var maxValue;
    
    try
    {
        using (SPSite objSite = new SPSite(sSiteUrl))
        {
            using (SPWeb objWeb = objSite.OpenWeb())
            {
                SPList objList = objWeb.Lists[sListName];
    
                SPQuery objQuery = new SPQuery();
                objQuery.Query = "1";
                objQuery.Folder = objList.RootFolder;
    
                // Execute the query against the list
                SPListItemCollection colItems = objList.GetItems(objQuery);
    
                if (colItems.Count > 0)
                {
                    maxValue = () colItems[0];
                }
            }
        }
    }
    catch (Exception ex)
    {
        ...
    }
    
    return maxValue;
    

提交回复
热议问题