score:10
Accepted answer
If you want the entire row with the highest date for each Id
, you can use the following code (written with LinqPad). If you just want the Id
, you can use @BurnsBA's answer, as it will be slightly more efficient.
void Main()
{
var data = new List<Record>
{
new Record(){Id=1, Value=1, Date=new DateTime(2017,1,1)},
new Record(){Id=1, Value=2, Date=new DateTime(2017,2,1)},
new Record(){Id=1, Value=3, Date=new DateTime(2017,3,1)},
new Record(){Id=2, Value=5, Date=new DateTime(2017,1,1)},
new Record(){Id=2, Value=6, Date=new DateTime(2017,2,1)},
};
var query = data.GroupBy(d => d.Id)
.SelectMany(g => g.OrderByDescending(d => d.Date)
.Take(1));
query.Dump();
}
public class Record
{
public int Id { get; set; }
public int Value { get; set; }
public DateTime Date { get; set; }
}
Results:
First it groups by Id
, then sorts the items within the group by Date
in descending order, and returns the first one, SelectMany
then flattens the list.
score:1
I suggest MoreLINQ's MaxBy
function, that is:
context.History.GroupBy( x => x.id ).Select( x => x.MaxBy( y => y.date) )
score:2
public class History
{
public int id { get; set; }
public int value { get; set; }
public DateTime date { get; set; }
}
// setup:
var values = new List<History>();
values.Add(new History() { id = 1, value = 1, date = DateTime.Parse("01/01/2017 20:20:20") });
values.Add(new History() { id = 1, value = 2, date = DateTime.Parse("02/01/2017 20:20:20") });
values.Add(new History() { id = 1, value = 3, date = DateTime.Parse("03/01/2017 20:20:20") });
values.Add(new History() { id = 2, value = 5, date = DateTime.Parse("01/01/2017 20:20:20") });
values.Add(new History() { id = 2, value = 6, date = DateTime.Parse("02/01/2017 20:20:20") });
// result :
values.GroupBy(
x => x.id,
y => y.date,
// Below, dates will be enumerable
(id, dates) => new { id = id, date = dates.Max() }
)
// returns enumerable collection of anonymous type:
{
{ id = 1, date = [3/1/2017 8:20:20 PM] },
{ id = 2, date = [2/1/2017 8:20:20 PM] }
}
Source: stackoverflow.com
Related Articles
- Select values with max date for each ID
- Select all objects with max date for each ID with Linq
- How to select only the records with the highest date in LINQ
- How to Select Min and Max date values in Linq Query
- Entity Framework select one of each group by date
- Select only the lowest values with Linq
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- How can I select items with Linq by Date while ignoring Time portion of a DateTime property?
- C# LINQ Select objects with the same value of one property join values of other
- select values tolist() when orderby distinct date
- C# Create object with dynamic properties : LINQ select List<object> values by property names array
- Group items and select specific item from each group with LINQ
- How to select each splited string and group by with another members in linq?
- LINQ select next record with each matching result
- How to make select query using IN condition with unknown number of values
- LINQ VB how to select records with the max date (highest date)
- LINQ to select a column with max date from a group
- Linq select distinct values with comparison
- How can i select all values from a nested dictionary with linq?
- Select Max, but only on numeric values with LINQ to Entities
- How to select all the values of an object's property on a list of typed objects in .Net with C#
- Linq Select projections with calculated values
- Linq Select with Where based on Dropdown values
- LINQ to SQL: Select count rows for each date in a provided range?
- LINQ building array with multiple values per select
- Linq to XML select descendent of descendant each with specific attribute
- Select values from elements with the particular class name
- Linq : Select multiple values and assign to select with specific option/value
- How to select distinct with linq when comparing values
- Select values in anonymous type with func in linq select statement
- Linq sql - where should I put phrase with "where"
- Use indexOf() function in LINQ in vb.net or C#?
- Efficient way to do a grouping, then a join to data from a different context/db
- linq how to select the parent from a collection where the parent contains child items in another collection
- Linq Query to get last entry of every year in the last x years
- why ToList() doesnt work in linq?
- linq check if a value exists in a list
- Compare keys of dictionary with values of List and return all the matching values including duplicates
- NullReferenceException Error when trying to iterate a IEnumerator
- Dynamically concatenate value in list if pattern matched
- Call a Linq to SQL user defined function multiple times in one trip to the DB
- Conditional Where in Linq using query syntax
- How to print after join linq query
- Return one Element from List using LINQ
- Linq statement returns null
- asp.net c# linq search List<object> tree
- Help with linq to sql query
- Linq query - conditional dependent columns from one table in .Net MVC Core 3.1, Entity Framework
- Can you combine multiple lists with LINQ?
- Check if List of Items exist in database and if not Add it to database