score:1
Accepted answer
I may have bad understood, but basically, you want the last created date for each distinct Lot ? If yes, then
db.Reports
.GroupBy(r => r.Lot)
.Select(g => g.OrderByDescending(x => x.createdDate).FirstOrDefault());
or to do like you (but I'm not sure it's the easiest way)
var maxDates = Reports.GroupBy(r => r.Lot)
.Select(x => x.Max(g => g.createdDate).ToList();
var result = db.Reports.Where (m => maxDates.Contains(m.createdDate));
EDIT
Your example is not that clear (I change a little bit the names for clarity).
With code first, you should have something like that (idea is the same)
a class Report
public class Report {
public int Id {get;set;}
public int Lot {get;set;}
public Datetime CreatedDate {get;set;}
public virtual Category Category {get;set;} //Navigation property
}
a class Category
public class Category {
public int Id {get;set;}
public string Title {get;set;}
public virtual IList<Report> ReportList {get;set;} //Navigation property
}
and eventually a "result class"
public class ReportList {
public string Title {get;set;}
public List<Report> ReportList {get;set;}
}
Then query could become
db.Reports
.GroupBy(r => r.Lot)
.Select(g => g.OrderByDescending(x =x.createdDate).FirstOrDefault())
.GroupBy(m => m.Category.Id)
.Select(g => new ReportList {
Title = g.FirstOrDefault().Category.Title,
ReportList = g.OrderBy(x => x.Lot)
});
score:2
var recentDates = Reports
.GroupBy(r=>r.SeedLot, r=>r.CreatedDate)
.Select(rg=>rg.Max());
var result =
from r in Reports
join d in recentDates
on r.createdDate equals d
select r;
Source: stackoverflow.com
Related Articles
- Converting SQL statement into Linq
- Converting SQL code with Row_Number() into LINQ C# code
- converting a c# linq statement into vb.net
- Converting a resource set into dictionary using linq
- How do I convert Foreach statement into linq expression?
- Convert nested for-loops into single LINQ statement
- Converting F# Quotations into LINQ Expressions
- Resharper reformatting Linq statement to put into and select on same line
- LINQ Source Code Available
- How can I combine this code into one or two LINQ queries?
- Converting a LINQ query into a Dictionary<string, string[]>
- Converting sql statement that contains 'with' cte to linq
- Converting a list of lists into a single list using linq
- Convert simple ForEach into a Linq statement
- Split list into two lists with single LINQ statement
- How to get SQL query into LINQ form in C# code
- Linq - converting query expressions into dot notation
- Converting SQL query into LINQ - not working
- Can you convert this Linq statement into Lambda without using join statements?
- creating Linq to sqlite dbml from DbLinq source code
- Help converting an sql query into LINQ
- Need help turning this LINQ expression into LINQ statement
- converting group SQL expression into LINQ
- How to flatten a multi level XML into a single level XML using c# code LINQ
- Converting a nested for loop into LINQ
- Convert piece of code into LINQ (short syntax)
- Converting conditionally built SQL where-clause into LINQ
- Converting foreach loop to LINQ query breaks code
- Can't add a new record with an integer value into database by using linq from code C#
- How can I code numerous MIN functions into one LINQ to DataSet query
- Change empty string to zero in list
- Left Join LINQ and using Bitwise Comparisons
- Inefficient 'ANY' LINQ clause
- Linq performance: Any vs. Contains
- Remove duplicates in custom IComparable class
- EFCore Filter on Property With Contains And List of Values
- Split into KeyValuePair instead of Array
- LINQ vs. nHibernate
- C# Linq Where Clause on Nested List Attribute
- How to return this result using linq in entity framework( 2 foreign key mapping table)
- How to Join two dictionary collections linq query
- C# Linq non-vowels
- How to parse XML file in C# using LINQ?
- How can I group by specific timestamp intervals in C# using LINQ?
- VB.NET to C# Linq
- how to use LINQ with dynamic paramters in orderby clause
- Join two Collections with LINQ with Contains operator
- How to typecast an imported class from an sql table
- Writing nested collection to datatable c#
- how to if variable is null select all record else select filtered record in linq