Advertisement
gaiththewolf

Untitled

Aug 18th, 2021 (edited)
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApp1
  12. {
  13.  
  14.     public class MyObject
  15.     {
  16.         private string id;
  17.         private DateTime date;
  18.  
  19.         public MyObject(string _id, DateTime _date)
  20.         {
  21.             this.id = _id;
  22.             this.date = _date;
  23.         }
  24.         public string Id
  25.         {
  26.             get { return id; }
  27.             set { id = value; }
  28.         }
  29.  
  30.         public DateTime Date
  31.         {
  32.             get { return date; }
  33.             set { date = value; }
  34.         }
  35.     }
  36.     public partial class Form1 : Form
  37.     {
  38.         public Form1()
  39.         {
  40.             InitializeComponent();
  41.         }
  42.  
  43.         private void Form1_Load(object sender, EventArgs e)
  44.         {
  45.  
  46.             List<MyObject> MyObjectList = new List<MyObject>();
  47.  
  48.             MyObjectList.Add(new MyObject("1", new DateTime(2021, 8, 30)));
  49.             MyObjectList.Add(new MyObject("2", new DateTime(2021, 8, 05)));
  50.             MyObjectList.Add(new MyObject("3", new DateTime(2021, 8, 28)));
  51.             MyObjectList.Add(new MyObject("4", new DateTime(2021, 8, 10)));
  52.  
  53.             //Methode 1
  54.             MyObject MaxObj1 =  MyObjectList.FirstOrDefault(x => x.Date == MyObjectList.Max(y => y.Date).Date);
  55.             MessageBox.Show(MaxObj1.Id);
  56.  
  57.             //Methode 2
  58.             MyObject MaxObj2 = MyObjectList.First(); // just initiation
  59.             for (var i = 0; i < MyObjectList.Count; i++)
  60.             {
  61.                 for (var j = 0; j < MyObjectList.Count; j++)
  62.                 {
  63.                     if(MyObjectList[j].Date > MyObjectList[i].Date)
  64.                     {
  65.                         MaxObj2 = MyObjectList[j];
  66.                         break;
  67.                     }
  68.                 }
  69.             }
  70.             MessageBox.Show(MaxObj2.Id);
  71.  
  72.             //Methode 3
  73.             MyObject MaxObj3 = MyObjectList.First(); // just initiation
  74.             foreach (MyObject obj in MyObjectList)
  75.             {
  76.                 foreach (MyObject obj2 in MyObjectList)
  77.                 {
  78.                     if(obj2.Date > obj.Date)
  79.                     {
  80.                         MaxObj3 = obj2;
  81.                         break;
  82.                     }
  83.                 }
  84.             }
  85.             MessageBox.Show(MaxObj3.Id);
  86.         }
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement