Find latest record from list based on object property?

get max datetime from list c#
how to get latest date from list in java
c# find latest date in list
get max date from list c#
linq get most recent record
linq get latest record by date
get min date from list c# using linq
get max date from list c# using linq

I have a list of class Test

public class Test
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }

List have below data

List<Test> lstTest = new List<Test>();
lstTest.Add(new Test() { Id = 1, Name = "Test 1", CreatedDate = Convert.ToDateTime("05/05/2005"), ModifiedDate = Convert.ToDateTime("09/05/2005") });
lstTest.Add(new Test() { Id = 2, Name = "Test 2", CreatedDate = Convert.ToDateTime("06/05/2005"), ModifiedDate = Convert.ToDateTime("07/05/2005") });
lstTest.Add(new Test() { Id = 3, Name = "Test 3", CreatedDate = Convert.ToDateTime("08/05/2005"), ModifiedDate = null });

now I want to find a record which is updated last for example in the list of Test three records first record is latest updated because its modified date is latest so how can I find it?

now lets change scenario

lstTest.Add(new Test() { Id = 4, Name = "Test 4", CreatedDate = Convert.ToDateTime("10/05/2005"), ModifiedDate = null });

so 4th record is latest based on created date so how we can find 4th record Thanks

Well, you could sort the list by date and use LINQ to get the record:

var latest = lstTest.OrderByDescending(x => x.ModifiedDate ?? x.CreatedDate).First();

How do I get the latest date from a collection of objects using LINQ , DateTime? latestDate = myCollection.Max(r => r.ExpirationDate);. Intellisense should have given you the Max() method. =) Now, if you must  Dim source1 As New BindingSource () source1.DataSource = view1 ' Set the data source for the DataGridView. datagridview1.DataSource = source1 ' Set the Position property to the results of the Find method. Dim itemFound As Integer = source1.Find ("artist", "Natalie Merchant") source1.Position = itemFound End Sub.

You may try this code:

var latestrecord = lstTest.OrderByDescending(t => t.ModifiedDate) .FirstOrDefault();

Access 2003 Personal Trainer, indexing fields, 122 Input Mask characters, 144 Input Mask field property, 121 Input 17 cut, 17 data types, 67 delete record, 17 find, 17 Find and Replace, 96 find and 78 Last option (Total), 185 Last Record, 25 Layout for Print property, 227 Left embedded objects, 330 linking tables, 322 list boxes, 14 Locked property,  Exists () is just like Find on List except it returns true or false, not a new List of matches. It can make some code simpler. Find () and Exists () makes some C# code clearer. Find example. Here we consider the Find () method on List. Find accepts a Predicate, which we can specify as a lambda expression.

You can use LINQ OrderBy to sort by date (oldest first), then use LastOrDefault to get the last entry (or null if the collection contains no entries).

Use null coalescing operator ?? to handle any null values of ModifiedDate and use the CreationDate in this case for comparison.

Test lastEntry = lstTest
    .OrderBy(t => t.ModifiedDate ??  t.CreatedDate)

C# Fiddle

ORACLE DEVELOPER 2000: Basics to Implementation, A list of commonly used item interaction trigger is mentioned in Fig. 5.1. The Object Navigator and the Layout Editor of the application form is shown in The application will be based on CUST table. LAST RECORD and EXIT FIND and DISPLAY will be used to find and display Also set property Mouse Navigate — No. If you need to find the index of a value, use Array.prototype.indexOf(). (It’s similar to findIndex() , but checks each element for equality with the value instead of using a testing function.) If you need to find if a value exists in an array, use Array.prototype.includes() .

try this ... then you can find latest record.

var latestRecord = lstTest.OrderByDescending(x => x.ModifiedDate ?? x.CreatedDate).FirstOrDefault();

Active Record Query Interface, After reading this guide, you will know: How to find records using a variety of To retrieve objects from the database, Active Record provides several finder methods. last will return the last record ordered by the specified attribute for order . if you wanted to run a batch process using a subset of records based on :start  JavaScript : find an object in array based on object's property (and learn about the "find" function) Published on March 20, 2017 March 20, 2017 • 309 Likes • 48 Comments Report this post

Databases: Level 2 Diploma for IT Users for City and Guilds, Properties of controls Add/delete a field on a form Header and footer Image on a table, form, report or query (.lose Access Select the object you want to alter, the form, To add a field, show the field list (View, Field list) and drag the field on to the form. Base a report on a query so that you can select records for printing. The Select-Object cmdlet selects specified properties of an object or set of objects. It can also select unique objects, a specified number of objects, or objects in a specified position in an array. To select objects from a collection, use the First, Last, Unique, Skip, and Index parameters. To select object properties, use the Property parameter.

Active Record Query Interface, class Address < ActiveRecord::Base belongs_to :client end To retrieve objects from the database, Active Record provides a class method called Model.find. This method allows you to pass Model.find(:last, options) is equivalent to Model.last(​options) ActiveRecord::MissingAttributeError: missing attribute: <attribute>. I need to sort a list of ReadingValue based on the DateTimeOfReading property. This is what I've come up with: public IEnumerable<ReadingValue> Hourly(IList<ReadingValue> readings) { var sortedReadings = readings.OrderBy(x => x.DateTimeOfReading.TimeOfDay) .OrderBy(x => x.DateTimeOfReading.Date) .OrderBy(x => x.DateTimeOfReading.Year); return sortedReadings; }

List<T>.Find(Predicate<T>) Method, The following example demonstrates the Find method on a List<T> that to XML to parse the values from the XML to property values of the book objects. FindLastIndex(Predicate<T>), Finds the index of the last computer book using the​  var c = from d in CustomerOrders select d.OrderLines.ToList (); IEnumerable<string> b = from q in (c as List<OrderLine>) select q.ProductCode; The first line is fine, but the next one causes an exception becase c is null. I didn't even get to trying to find the distinct values from the list :) Help and tips much appreciated! Posted 3-Dec-09 9:18am.

  • Hi @fabjan there are possibility modified date will be null and when modified date is null then need to look in created date
  • @JasminSolanki Updated my answer
  • Asking "latest updated because its modified date is latest". So order by ModifiedDate
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.