Search This Blog

Monday, January 19, 2015

SPQuery to get Highest salary from Custom List in SharePoint

Here is very simple example to get highest salary of employee from Custom List using SPQuery...


1- Create a list as given below...



using (SPSite site = new SPSite("http://localhost"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists["EmployeeList"];
                        SPQuery query = new SPQuery();
                        query.Query = "<OrderBy><FieldRef Name='Salary' Ascending='False' /></OrderBy>";
                        query.RowLimit = 1;
                        SPListItemCollection items = list.GetItems(query);

                        if (items.Count > 0)
                        {
                            SPListItem _item = items[0];

                            Console.WriteLine("Name: " + _item["Name"].ToString() + "\t" + "Salary: " + _item["Salary"].ToString());

                        }
                    }
                }

               
Output given below:















No comments:

Post a Comment