Example E468
Visible to All Users

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

Files to look at:

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.

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

ConsoleApplication1/Program.cs
C#
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"); } } } }
ConsoleApplication1/Quote.cs
C#
using DevExpress.Xpo; namespace ConsoleApplication1 { public class Quote : XPObject { public Quote() : base() { // This constructor is used when an object is loaded from a persistent storage. // Do not place any code here. } public Quote(Session session) : base(session) { // This constructor is used when an object is loaded from a persistent storage. // Do not place any code here. } public override void AfterConstruction() { base.AfterConstruction(); // Place here your initialization code. } private string _name; public string Name { get { return _name; } set { SetPropertyValue("Name", ref _name, value); } } [Association("Quote-QuoteItems"), Aggregated] public XPCollection<QuoteItem> QuoteItems { get { return GetCollection<QuoteItem>("QuoteItems"); } } } }
ConsoleApplication1/QuoteItem.cs
C#
using DevExpress.Xpo; namespace ConsoleApplication1 { public class QuoteItem : XPObject { public QuoteItem() : base() { // This constructor is used when an object is loaded from a persistent storage. // Do not place any code here. } public QuoteItem(Session session) : base(session) { // This constructor is used when an object is loaded from a persistent storage. // Do not place any code here. } public override void AfterConstruction() { base.AfterConstruction(); // Place here your initialization code. } private int _sequence; public int Sequence { get { return _sequence; } set { SetPropertyValue("Sequence", ref _sequence, value); } } private Quote _quote; [Association("Quote-QuoteItems")] public Quote Quote { get { return _quote; } set { SetPropertyValue("Quote", ref _quote, value); } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.