score:1
One can apply the filter in very beginning itself. Use group by
with into
and then finally select what you need. Since its creating a anonymous object hence it would be better to start with 'var`.
Edited: Fetch the data in memory and then apply group by
to avoid any problem in mapping with database side grouping.
var list = (from p in _DBcontext.Events
where p.acteename == item.AppName select p).ToList();
var fList = (from p in list
group p by p.acteename
into gList
select new
{
acteenamex = gList.Key,
lastUpdated_at = gList.OrderBy(x => x.updated_at).Last().updated_at,
diff = gList.OrderBy(x => x.updated_at).Last().count -
gList.OrderBy(x => x.updated_at).First().count
}).ToList();
score:1
You can filter the list by the AppGuid
first, then order it, and then convert that to list. Now, use this list in a new variable to get the records you need from it.
var gList = _DBcontext.AppProcessCrashEvents
.Where(x => x.Actee == item.AppGuid)
.OrderByDescending(p => p.UpdatedAt)
.ToList();
var result = new Events
{
Actee = gList[0].Actee,
UpdatedAt = gList[0].UpdatedAt,
CrashCount = gList[0].CrashCount - gList[gList.Count - 1].CrashCount
};
I hope this is what are you looking for.
Source: stackoverflow.com
Related Articles
- LINQ how to return last date and difference between first and last count
- Difference between value of first and last record Linq
- What is the difference between First and FirstOrDefault , Last and LastOrDefault
- Linq return first date
- linq difference between two select count queries
- Getting first time-In and last time-Out covering the following day based on a date Collection using linq
- To get the count of working days between two date in linq
- Get count of unique characters between first and last letter
- Count between range of date bind in gridview. LINQ to SQL Visual Basic
- LINQ return Mixed Date Between Two Tables With Many-To-One Relation
- What is the difference between LINQ ToDictionary and ToLookup
- C# Linq Where Date Between 2 Dates
- Get the difference between two lists using LINQ
- Linq - Grouping by date and selecting count
- Linq way to get piecewise difference between element and next element in list
- Take the first five elements and the last five elements from an array by one query using LINQ
- Performance Difference between LINQ and Stored Procedures
- Difference between Linq to Sql, Linq, Typed Datasets, ADO.NET
- What is the difference between LINQ query expressions and extension methods
- Difference between Sum and Aggregate in LINQ
- why is this linq query return a boolean and not the first result of the select?
- Difference between cast and as inside a select in LINQ
- Using C# Linq to return first index of null/empty occurrence in an array
- Logical difference between LINQ Where() and FirstOrDefault()
- How to convert between Linq expressions with different return types?
- Why is there a performance difference between LINQ (c#) vs Seq (f#)
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Difference between LINQ Queries & Lambda expression
- Group by date range , count and sort within each group LINQ
- What's the difference between these LINQ queries?
- How can i order by Current Date in Linq/Sql?
- Get time only of datetime
- Remove rows where column contains specific text
- Entity Framework missing ObjectSet methods
- Linq query to return the cheapest product, only if it is unique
- Why doesn't IOrderedEnumerable retain order after where filtering
- How to force Linq to join tables
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- Convenient way to drop empty strings from a collection
- Why does this LINQ query assign a value of 1 to a NULL value from the database?
- C#: XML To Type -LINQ
- LINQ GroupBy or something else for a single loop?
- Get Individual Values From Foreach Loop and Sum All
- How does OrderBy work with regard to strings in C#?
- Join two lists of different objects and create a new list
- Help Converting T-SQL Query to LINQ Query
- How can I create a conditional where clause using LINQ
- Having problems converting conditional where clause in LINQ back over to SQL
- horrible and big LINQ statement optimisation
- Lambda Query on Generic Type T