score:7
For elegance, i would go by sorting the set based on the datetime field and returning the first item like:
set.OrderByDescending(x => x.DateTime)
.FirstOrDefault();
This will create an in-memory representation of the sorted collection so efficiency is not that good. The most efficient solution for an unsorted set would be to loop over all items and save the newest. You can use linq for this by performing an aggregate operation which i find syntactically a mess.
Alternatively, you can store your items in a sorted collection like the SortedSet. This has a bit more complex insertion time 0(log2) instead of O(1) for most collections but it allows you to immidiatly sort on datetime and therefore selecting the newest item in O(1) rather than O(n).
score:0
Try Aggregate, i.e. something like:
list.Aggregate (
DateTime.MinValue,
(lastOne, current) => current.GreaterThan (lastOne) ? current : lastOne
)
i.e. if your field is DateTimeField, you should write something like
list.Aggregate (
null,
(lastOne, current) =>
(lastOne == null) ||
current.DateTimeField.GreaterThan (lastOne.DateTimeField)
? current
: lastOne
)
score:3
Try that:
var newItem = myList.OrderByDescending(item => item.yourDateTimeField).First();
score:0
try this one
sortlist.OrderByDescending(a => a.timeStamp).First();
score:7
Most of the solutions so far have to completely sort the list first (via OrderByDescending), which is unnecessary and time consuming. What you want is Jon Skeet's MoreLinq MaxBy
function. Source for MaxBy is on google code.
var newest = thelist.MaxBy(x => x.DateTimeField);
Source: stackoverflow.com
Related Articles
- Find the newest item in a C# generic list using Linq
- Using LINQ to find item in a List but get "Value cannot be null. Parameter name: source"
- How to find the last item from list have the same item using linq
- Find list of values for item using Linq
- Find the position of a List Item in a List based upon specifice criteria using LINQ
- I need to find max total of the sum of integers in Generic list using Linq
- Using linq to find the cheapest item in a list thats greater than 0
- find item in list , and get other item from other list at same location using linq
- Find an item in a list by LINQ
- Find an item in a generic list by specifying multiple conditions
- Find first element of certain type in a list using LINQ
- Simplest way to filter value from generic List in C# using LINQ
- Remove item from list using linq
- Find child objects in list of parent objects using LINQ
- Count Occurrences of an Item in a List using LINQ
- Remove a specific item from a list using LINQ
- Auto-incrementing a generic list using LINQ in C#
- Find the Second Max in a list of values using linq c#
- How to find the first item according to a specific ordering using LINQ in O(n)?
- Checking for item in Generic List before using it
- Is it possible to ignore a list item using the Skip method of the LINQ then apply skip on the list without losing that item?
- How to find the index of next matching element in list using LINQ
- Find item from generic list
- Using Linq select an item outside an List
- Using Linq to find next element in subset of a list
- Remove child list item when checking grandchild list using Linq
- Using Linq find first object in list sorting by property A, then property B
- Find indices of particular items in the list using linq
- Using LINQ to get a list of items where the item contains a part of an item from another list
- When searching for an item in a generic list should I use LINQ or Contains?
- Need help understanding how to convert sql statement to (Linq) or (Linq To SQL)
- What's the most (performance) efficient and readable way to 'split' a generic list based on a condition?
- Linq GroupJoin add default entries
- Is it possible to use LINQ to check if all numbers in a list are increasing monotonically?
- Filtering issues in grid
- Linq, EF Core - group by on one field and use other field to get list of data from other table
- What is the equivalent T-SQL select query for the attached linq query?
- Concatenate ids based off of same Name in Entity Framework/Linq
- How to compare same property value regardless of order or duplication in multiple models
- how can Linq have a table's fields selected dynamically by a variable?
- Combine Parent and Child Lists into a single nested Parent List
- How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items
- SQL Server ADO.NET Query conversion to Linq in WCF service
- How can I get a random record in LINQ to SQL when Guid doesnt work
- SQL to LINQ Query with date in where clause is not working
- Linq Query Return List of Records instead of displaying single record
- Can't get Dynamic Linq to work
- Linq - how to group by a 'let' value and sum another 'let' value
- .NET Core Expression DbFunctions DiffDays
- List union that depends / selects on property values