Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApp1
- {
- public class MyObject
- {
- private string id;
- private DateTime date;
- public MyObject(string _id, DateTime _date)
- {
- this.id = _id;
- this.date = _date;
- }
- public string Id
- {
- get { return id; }
- set { id = value; }
- }
- public DateTime Date
- {
- get { return date; }
- set { date = value; }
- }
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- List<MyObject> MyObjectList = new List<MyObject>();
- MyObjectList.Add(new MyObject("1", new DateTime(2021, 8, 30)));
- MyObjectList.Add(new MyObject("2", new DateTime(2021, 8, 05)));
- MyObjectList.Add(new MyObject("3", new DateTime(2021, 8, 28)));
- MyObjectList.Add(new MyObject("4", new DateTime(2021, 8, 10)));
- //Methode 1
- MyObject MaxObj1 = MyObjectList.FirstOrDefault(x => x.Date == MyObjectList.Max(y => y.Date).Date);
- MessageBox.Show(MaxObj1.Id);
- //Methode 2
- MyObject MaxObj2 = MyObjectList.First(); // just initiation
- for (var i = 0; i < MyObjectList.Count; i++)
- {
- for (var j = 0; j < MyObjectList.Count; j++)
- {
- if(MyObjectList[j].Date > MyObjectList[i].Date)
- {
- MaxObj2 = MyObjectList[j];
- break;
- }
- }
- }
- MessageBox.Show(MaxObj2.Id);
- //Methode 3
- MyObject MaxObj3 = MyObjectList.First(); // just initiation
- foreach (MyObject obj in MyObjectList)
- {
- foreach (MyObject obj2 in MyObjectList)
- {
- if(obj2.Date > obj.Date)
- {
- MaxObj3 = obj2;
- break;
- }
- }
- }
- MessageBox.Show(MaxObj3.Id);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement