score:0
After searching and reading stackoverflow, make desired result with this code.
var groupedList = (from t in list
group t by new { t.Account, t.StartDate } into g
select new
{
g.Key.Account,
g.Key.StartDate
});
var filteredList = groupedList.GroupBy(x => x.Account)
.SelectMany(g => (from t in g orderby t.StartDate descending select t)
.Take(2).ToList() );
var Result = (from c in list
join k in filteredList on
new { c.StartDate, c.Account } equals
new { k.StartDate, k.Account } //into j
select c).ToList();
/* or lambda method chain
var groupedList = list.GroupBy(t => new {t.StartDate, t.Account})
.Select(g => new { g.Key.StartDate,g.Key.Account})
.GroupBy(x => x.Account)
.SelectMany(g => (from t in g orderby t.StartDate descending select t)
.Take(2).ToList() );
var result = (list.Join(inner: groupedList,
outerKeySelector: c => new {c.StartDate, c.Account},
innerKeySelector: k => new {k.StartDate, k.Account},
resultSelector: (c, k) => c))
.OrderByDescending(e =>e.StartDate).OrderBy(e =>e.Account).ToList();
*/
Console.WriteLine(Result);
Thanks a lot LINQPAD(the best tool for linq) and all friends in stackoverflow (the best and professional developers in the world)
but i guess my code is very complex (3 level filtering) and have not best performance. :)
who have a better offer, please let me know.
I'd love to get some improvements!
score:0
In order to get the top two from the group the query would be like this: Update But in this case, the combination of the Account ID and the Start Date must be unique.
.ToList().GroupBy(x=>new{x.Account,x.StartDate}).SelectMany(y=>y.OrderByDescending(z=>z.StartDate).Take(2));
I am using the similar one in my code and know this works fine.
score:0
At last i find one statement which produce desired result.
var result = (from cheque in list.OrderBy(a => a.Account).ThenByDescending(a => a.StartDate)
group cheque by new { cheque.Account, cheque.StartDate } into gr
//from ids in gr
group gr by gr.Key.Account into secondGrouping
from second in secondGrouping.Distinct().Take(2)
from Cheque f in second
select f
).ToList<Cheque>();
score:2
The trick is to group by Account
and Serial
. Take the top two dates and then flatten the list again by SelectMany:
list.GroupBy(x => new {x.Account, x.Serial})
.Select(g => new { FirstTwo = g
.GroupBy(x => x.StartDate).Select(x => x.FirstOrDefault())
.OrderByDescending(x => x.StartDate).Take(2)
})
.SelectMany(g => g.FirstTwo)
.OrderBy(x => x.Account)
.ThenByDescending(x => x.StartDate)
.ThenBy(x => x.Serial)
Result:
1 1 20120120
1 2 20120120
1 3 20120120
1 1 20110120
1 2 20110120
1 3 20110120
2 1 20120314
2 2 20120314
2 1 20100417
2 2 20100417
Source: stackoverflow.com
Related Query
- Entity Framework - Linq : how query to take 2 first ordered and grouped list
- Entity Framework Linq Query to List - Error when using contains: Only primitive types, enumeration types and entity types are supported
- How to get a distinct, case-insensitive list using Linq and Entity Framework
- How to query many-to-many relationship with 'AND' condition using LINQ and Entity Framework
- SQL subquery result in LINQ and Entity Framework Code First
- How to Query Icollections of Entity Framework Code First Data
- Create a entity framework LINQ group join query to return a subset of properties from DTO and a list of another DTO, ASP.net core
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- How to create this query using Entity Framework and Linq
- How to loop through a child list of objects in Entity Framework 4.1 code first
- How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core
- how to select data by linq in many-to-many relationship in First code Entity framework 5
- How to get a list from a linq to Entity Framework query using vb.net
- LINQ - Entity framework code first - Grouped results to custom class
- Entity Framework LINQ query to return a list and subset of collection satisfying certain condition
- Entity Framework 4 and Linq to Entities specifications: How to code it?
- how to get a list of item in object from another table using linq and entity framework in C#?
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- How to write linq query with list of two joined tables a and B, then grouped by id of a, and with amount of joined rows from B
- Entity Framework Code Most First Efficient Linq Query
- Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query
- How to return just first parent element, but many children and grandchildren in Linq with Entity Framework - SYBASE ASE Connector bug
- How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?
- Take the first five elements and the last five elements from an array by one query using LINQ
- How to avoid memory overflow when querying large datasets with Entity Framework and LINQ
- Entity Framework - Linq query with order by and group by
- Convert string to int in an Entity Framework linq query and handling the parsing exception
- How can I view the Entity Framework LINQ query plan cache?
- How to get "remainder" of query (all non-matched pairs) in LINQ and collect it in a list
- How to add left outer join to grouped and summed LINQ query
More Query from same tag
- How to order application settings
- EF6 - Include objects with List<Func<T, object>>
- Select -> Refer to itself (currently created list)
- AppendText method in C# (Windows Forms)
- Order by not working in LINQ query
- Multi-dimensional arrays in Linq
- How to return a sorted array without storying it (immutability introduction)?
- Is it expensive to parse an ExpressionTree?
- Iterate through LINQ Results List
- NHibernate Futures using Linq to query count
- Linq to entities does not recognize the method system.string get_Item
- How do I get the next record in table with linq?
- LINQ Select distinct on DataTable not working
- How to make Distinct... More distinct?
- The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.01
- Entity Framework select object with grandparent ID
- LINQ and GroupBy
- LINQ Select Query with Where Condition Which returns single record
- how do you add a condition to a lambda expression
- How can I get just one column from a table?
- Web API 2. Return Description instead of Int Join/Include?
- GroupBy Linq IEnumerable error
- LINQ/Code first: how do I delete a child object's reference from the parent object?
- Using LINQ, where one object property has a value of x, how do I return the value in a different property
- List of Many-To-Many relationship EF Code-First
- How do I select multiple grandchildren in linq?
- How to convert SQL query to LINQ with Orderby, Groupby
- VB.NET XML Linq Get Descendants
- Simple sql to Linq query with group by and aggregate functions
- LINQ complex delete and search query