Current filter:
                                You should refresh the page.
                                Support Center

                                How to get the maximum or minimum object's value from a collection

                                0

                                This example demonstrates how to find the object with the highest value from a collection. Here the Quote class has a collection of QuoteItem objects.

                                You must  log in  or  register  to leave comments
                                Select file
                                • Program.cs
                                • Quote.cs
                                • QuoteItem.cs
                                Select language
                                • C#
                                • VB.NET
                                Select version
                                • v2013 vol 1.4 - v2013 vol 2.9
                                • v2008 vol 3.2 - v2012 vol 2.16
                                using System;
                                using System.Diagnostics;
                                using DevExpress.Xpo;
                                using DevExpress.Data.Filtering;
                                using DevExpress.Xpo.DB;
                                
                                namespace ConsoleApplication1 {
                                    class Program {
                                        static void Main(string[] args) {
                                            Quote q = Session.DefaultSession.FindObject<Quote>(new OperandProperty("Oid") == 1) as Quote;
                                            if (q == null) {
                                                q = new Quote();
                                                for (int i = 0; i < 5; i++) {
                                                    QuoteItem qi = new QuoteItem();
                                                    qi.Sequence = i;
                                                    q.QuoteItems.Add(qi);
                                                    qi.Save();
                                                }
                                                q.Save();
                                            }
                                            int maxValue = Convert.ToInt32(
                                                Session.DefaultSession.Evaluate<QuoteItem>(
                                                    CriteriaOperator.Parse("Max(Sequence)"),
                                                    new OperandProperty("Quote.Oid") == q.Oid
                                                )
                                            );
                                            Debug.Assert(maxValue == 4, "Error");
                                
                                            XPCollection<QuoteItem> items = new XPCollection<QuoteItem>(
                                                new OperandProperty("Quote.Oid") == q.Oid,
                                                new SortProperty("Sequence", SortingDirection.Descending)
                                            );
                                            items.TopReturnedObjects = 1;
                                            if (items.Count != 0) {
                                                QuoteItem maxQuoteItemObject = items[0] as QuoteItem;
                                                Debug.Assert(maxQuoteItemObject.Sequence == 4, "Error");
                                            }
                                        }
                                    }
                                }

                                To start a chat you should create a support ticket


                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners