You can use
var products = _context.Product .Where(x => x.IsActive) .GroupBy(x => x.SecondaryId) .Select(grp => grp.OrderByDescending(x => x.CreatedOn).First()) .ToDTOs();
Then within each group, select the object with highest
You'll need to group by the Secondary ID's, then take only the first row from each grouping, like so:
var products = _context.Product .Where(x => x.IsActive) .GroupBy(x => x.SecondaryID) .Select(x => x.OrderByDesc(y => y.CreatedOn).First()) .OrderBy(x => x.CreatedOn) .ToDTOs();
EDIT: missed that you wanted specifically the newest from each grouping - the OrderByDesc in the Select statement should do the trick
- How to limit a LINQ query based on date and a secondary ID
- How to Select Min and Max date values in Linq Query
- How to write a generic LINQ query for filtering of data based on first name middle name and last name
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Linq query to order a list based on date and get distinct
- How to get date and total date in Linq query
- How to select a column based on date condition in LINQ query
- Linq query to get distinct object based on date and priority
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to assign LINQ Query to a variable and then use it later in the code
- LINQ query on dotnet code and how concat will work here
- How do I remove items from generic list, based on multiple conditions and using linq
- How to dynamically build and return a linq predicate based on user input
- How to intercept and modify SQL query in Linq to SQL
- how to take 100 records from linq query based on a condition
- how do i use group by only on date from datetime in LINQ query
- how do I write LINQ query to retrieve distinct records based on only specific properties?
- linq - how do you do a query for items in one query source that are not in another one?
- How can I write the following code more elegantly using LINQ query syntax?
- How to get "remainder" of query (all non-matched pairs) in LINQ and collect it in a list
- How can I code an outer join using LINQ and EF6?
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- How to query many-to-many relationship with 'AND' condition using LINQ and Entity Framework
- Linq query filter by date (Month and year)
- How to get SQL query into LINQ form in C# code
- How to get the value of class properties with Linq query and IEnumerable
- How to add left outer join to grouped and summed LINQ query
- How to I read a json file containing array with C# and perform LINQ query on it?
- How linq to nhibernate by join and Separate where in query
- How make LINQ query with GroupBy and LEFT JOIN
More Query from same tag
- Need a subquery in LINQ
- Get new indices of items in a collection after sorting using LINQ
- Linq-to-SQL Load 1:1 Relations in a single query
- LINQ anonymous type to object()
- How to union two complex queries querying complex objects?
- Consolidate or reuse LINQ expression
- Nullable DateTime from SQL database into Datatable using generics and Linq
- Linq query to return parent or most recent child
- C#, Winforms & LINQ to SQL.. Datacontext lifecycle?
- How to solve error in LINQ: Data is null or empty
- Searching For A Record After A LINQ Query Exception
- RIA Servcies Filter Out Included Entities
- How to convert a List<T> into a comma-separated list, using the class's Id property as the value
- Lexicographical sort algorithm using C#
- Search for combination of two items with Linq
- Is that a shorter way assigning properties with LINQ Query Syntax?
- Error Reading RSS Feed using LINQ to XML
- Subsonic/Linq Not a sequence error what does it mean
- LINQ query to find items in a list from other list by item
- NullArgument Exception in LINQ query
- Linq to objects multiple statements vs. single statement
- System.IObservable<string> to System.IObservable<System.Collections.Generic.List<string>>
- List to Dictionary with incremental keys in one line LINQ statement
- Entity Framework - Group and Min
- linq sum and group
- Linq duplicate removal with a twist
- LINQ calling a Stored Procedure is slow
- Cannot convert from table to class
- How to programmatically set label text - Linq To Entities
- select the nodes that pass the selection of two subnodes with Linq in C#