score:2
Accepted answer
How about this?
var q1 =
from req in myEntity
group req by req.TaskId into g
let topReq = g.OrderByDescending(r => r.Timestamp).FirstOrDefault()
select new
{
topReq.TaskId,
topReq.SubTaskId,
topReq.Timestamp
};
score:1
var tasks = new[] {
new { TaskId = 1, SubTaskId = "A", Timestamp = DateTime.Parse("2010-11-22 15:48:49.727")},
new { TaskId = 1, SubTaskId = "B", Timestamp = DateTime.Parse("2010-11-22 16:24:51.117")},
new { TaskId = 2, SubTaskId = "C", Timestamp = DateTime.Parse("2010-11-15 11:49:05.717")},
new { TaskId = 2, SubTaskId = "D", Timestamp = DateTime.Parse("2010-11-15 14:02:11.467")}};
var newestTasks =
from task in tasks
group task by task.TaskId into g
select g.Where(t => t.Timestamp == g.Max(t2 => t2.Timestamp)).Single();
foreach (var task in newestTasks)
Console.WriteLine("TaskId = {0}, SubTaskId = {1}, Timestamp = {2}",
task.TaskId, task.SubTaskId, task.Timestamp);
Source: stackoverflow.com
Related Query
- How to get the max timestamp and groupby another field in LINQ to Entities
- How to group by multiple fields and get the count of another field
- Linq How to get the master record and a specific field of inner details record
- How to sum a field for all records and then return the max of all summed records with Linq
- How to get the name of elements/attributes in xml files and write these into another new xml file using LINQ
- LINQ Basics, How do I do a groupby and get a string at the end?
- How to use Linq or Lambda with IQueryable to GroupBy and get the first/last record on the collection in C#?
- How to get the index of a string value in a list, and assign it to a field in linq
- How can I get LINQ to return the object which has the max value for a given property?
- How do I get the MAX row with a GROUP BY in LINQ query?
- How do I get the max ID with Linq to Entity?
- How to get the item before current and after current in a dictionary with Linq / C#?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- How to get the latest date inserted from the table with Linq to entities
- LINQ with subselect and groupby to get only the latest version of each item in a list
- How can I get LINQ to return the index of the object which has the max value in a collection?
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- How to get the value of class properties with Linq query and IEnumerable
- How to get the sum of the volume of highest and lowest priced items in linq
- LINQ Query How to select Max value between start and end index and the index of the max value
- How to sort linq with fixed values and show all the rest with another sorting
- How can I get the top three players and their high scores from a collection using linq and lambdas
- How to Join in LINQ using Lambda expressions and get the result in DTO?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to get the distinct record name and its each record count to bind to gridview in C# using Linq from SQL Server
- How to return unique column with the max version of another column in Linq to SQL?
- How can I do a LINQ query joining two entities and select what a Where on one of the entities?
- Why did I get an exception "Cannot implicitly convert type 'bool' to 'long?'" when using the LINQ Sum method and how to fix it?
- Using Linq to object, how can I get one field's value based on another in the same list
- Get the max date from linq with mutiple joins and with multiple select fields
More Query from same tag
- Order a List<T> by Date, but it has a String ID, if that ID is repeated with different date in the collection with should appear below of it
- LINQ query - Multiple where with multiple sub queries based on int array
- Linq get sum of data group by date
- ToUpper() in an Expression.Call
- LINQ to entity, sort the subset of returned values
- Exclude an array in a LINQ expression that connects to CRM
- LINQ Queries with dynamic Order By
- There is Already an open DataReader that must be closed first
- LINQ throws error on using Regex.Split()
- How can i Insert into one table and Update another table using Linq
- How to sum the ordinal value of letters in a string
- How can I upsert an enumerable from another enumerable c#
- LINQ-related error when using simple CSharpCodeProvider in ASP.NET
- way to go from list of objects to SortedList<string,string> with two of the fields using LINQ
- Get Sum of Property from Generic list having another generic list
- Linq - Include and filter - nested collections
- LINQ query on datatable, join multiple selects
- List from aggregated duplicate objects where a string property concats the strings from the duplicate objects in C#
- Mutilevel include in C# Linq
- Linq: rebuild hierarchical data from the flattened list
- Selecting values from List
- verify cummulative sum verify property efficiently with LINQ
- Differences between a linq query and extension In a MVC 4 application
- How would I translate following T-SQL statement To Linq
- Operator '!=' cannot be applied to operands of type 'bool?' and 'int'
- Linq Return Type
- How to order items with LINQ?
- LINQ reflection with Host Level Trust
- how to change sql query to linq?
- Linq to Sql query with multiple aggregations