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 Articles
- How to get the max timestamp and groupby another field in LINQ to Entities
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- LINQ to Entities Join on Nullable Field where Null Implies "Match All"
- LINQ Source Code Available
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- How to include one specific row from another table with LINQ to Entities
- linq - how do you do a query for items in one query source that are not in another one?
- Using LINQ how do I create a List of one particular field of an entity from a collection entities
- LINQ Lambda efficiency of code groupby orderby
- Linq expression to get all items in a IList where one field is in another List
- creating Linq to sqlite dbml from DbLinq source code
- EF Code First - Linq to Entities Union EqualityComparer
- linq to entities changing database connection string in code
- linq groupby without time field vb.net
- code first approach error: the specified type member 'yyyxx' is not supported in linq to entities
- Linq use value from select new to calculate another field
- GroupBy using LINQ but base distinct on another column, and still be able to count other records
- Linq return a field or another
- Linq to Entities add Where clause for EXISTS lookup in another table
- LINQ to remove elements from one collection that don't match elements in another collection based on a field
- LINQ to Entities similar code
- source code for LINQ 101 samples
- Linq to entities to return a list that contains elements from another list
- ODAC / Oracle Equivalent to Linq to Entities GroupBy FirstOrDefault
- Most popular GroupBy with Linq to Entities
- How to select and count base on a condition of another field using dynamic linq core
- LINQ GroupBy Source = A Destination = B or Source = B and Destination = A
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Linq to entities / EF : join to tables on a decimal and a string field
- LINQ to Entities Question - All objects where all items in subcollection appear in another collection?
- LINQ to SQL displayed in console mode app
- Get data from database using linq query and display it in dynamically generated labels in repeater
- Convert IEnumerable object list to list of object arrays C#
- Performing a date comparison with a date field stored in the database
- Alphabetic GroupBy in Linq with a twist
- Filter dataset on JOIN based on other table value
- C# Linq seeing Audit Logout after every read
- Will First() perform the OrderBy()?
- Aggregate pairs in linq query
- how do I return a filtered list using linq?
- Data grid View ( winform ) not populating properly
- How do I access consecutive elements in an IQueryable<T> object?
- Cannot implicitly convert type 'System.Linq.IQueryable?
- Converting a Parallel.ForEach loop into parallel tasks using lambda expression
- Difference between Where and Single
- NHibernate - LINQ Query Many to Many Issue
- WebAPI .getJSON function and querying a list with linq returning "Undefined"
- Nested query linq to entities
- SQL to LINQ Group by convertion
- C# CSV dynamic split