score:1
Accepted answer
try this
var query = _memberRepository.Query()
.Where(x => x.IsActive == true && x.OrgId == orgId)
.Include(x => x.BillDetails.Where(y => y.IsActive == true))
.AsNoTracking()
.ToList();
var memberBills=new List<MemberBill>();
foreach(var m in query)
{
memberBills.Add( new MemberBill
{
MemberId= m.Id,
BillAmount=m.BillDetails.Sum(i=> i.Amount)
});
}
score:1
Leaving here performant solution. Current accepted answer uses a lot of memory and server resources.
var query =
from m in _memberRepository.Query()
where m => m.IsActive == true && m.OrgId == orgId
from bd in m.BillDetails.Where(bd => bd.IsActive == true)
group bd by new { m.Id } into g
select new MemberBill
{
MemberId = g.Key.Id,
BillAmount = g.Sum(x => x.Amount
};
var memberBills = query.ToList();
Source: stackoverflow.com
Related Query
- Select list of objects from navigation property in Entity Framework Core
- Entity Framework select few fields from navigation property object
- Convert List of Different Objects from DB to List of object in Entity Framework Core
- How to select objects from a list that has a property that matches an item in another list?
- Entity Framework - Querying from ObjectContext vs Querying from Navigation Property
- How can I return if an optional navigation property exists from an Entity Framework Query?
- 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
- Select objects from a list using LINQ based on a property
- How to loop through a child list of objects in Entity Framework 4.1 code first
- How to get a list of objects from Entity Framework database?
- How to query Entity Framework objects where select is a list
- How to query the average from a certain property in an Entity and return it in a List of List of objects in LINQ?
- Entity Framework Core Select Property with multiple conditions
- Using a Linq query to select objects where any value of a property matches values from a list
- Entity Framework Core Invoke an Expression on Navigation property
- return a list of objects ordered by number of items in navigation property with linq to entity
- .Net Core 5 Rest Api Entity Framework Linq Expression Translation Error While Getting Record List From Controller
- Entity Framework Linq Query: How to Where on Multiple Nav Properties and Select from 3rd Nav Property
- making a list of int from navigation property field of an Entity
- How to get a list of user dialogs from the database of all messages using Entity Framework Core
- Linq Lamda expression to select multiple columns from entity class and its navigation property
- LINQ select one field from list of DTO objects to array
- Linq select object from list depending on objects attribute
- Select a specific property from array of objects angular
- How can I extract a list of Tuple from a specific table with Entity Framework / LINQ?
- Getting unique values from a list of objects with a List<string> as a property
- Entity Framework - Selective Condition on Included Navigation Property
- Using async with Entity Framework select list of type IQueryable<T>
- How to prevent Entity Framework from loading all child objects
- Select property of an object that is in a list of objects which is also in another list of objects
More Query from same tag
- How to GroupBy based on condition
- Percent of Yes in Yes/No column in Linq
- Get data from a table after joining based on null value of joined table using LINQ
- C# LINQ Left Outer Join for XML doesn't work properly
- Can't "Enable Editing" on my gridview
- Guidance on designing a solution - XML files vs database
- How can I order a List of Dictionary<int, string> based on Dictionary int value?
- How to Remove Range after Add Range in same operation
- Dynamic library (System.Linq.Dynamic) SQL LIKE Operator
- Calculating average for last X number of records
- C# laziness question
- How to use group by on multiple columns with Max function
- How to remove a child node with a null parent node in XML
- IEnumerable<string> to string
- Selecting item from an IEnumerable
- How can I get the average of the elements of a generic list that meet a criteria?
- How can I use a view of my database in a razor view?
- How can I assign a multiple column in my sql tables to single column in my List<>
- What makes "SqlMethods" Methods Work?
- Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?
- User Defined Table Type not working with Linq
- Need help with a simple Linq query running with Entity Framework 4
- How to get the values of this xml using linq?
- Check if string list contains any enum string value
- Find the most frequent numbers in an array using LINQ
- Order by fields in an anonymous type
- Joining multiple tables using Entity Framework
- LINQ SQL join multiple n-to-n relations and filter
- ForEach loop with Lambda expression in Razor code MVC 5 For List<T>
- C# - Using a Predicate to select a property