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 Articles
- 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
- How to select first n number of columns from a DataTable
- Linq To DataSet getting error
- Get names of properties which are not null
- Getting an error specified method is not supported
- Reading inner list only with LinqToXml
- using a linq query to filter from a list
- Unable to fix 'The object cannot be deleted because it was not found in the ObjectStateManager.'
- Enumerable.Zip alternative solution in Dot Net 3.0
- How to SELECT WHERE NOT EXIST using LINQ?
- Grouping by multiple columns
- LINQ Query Determine Input is in List Boundaries?
- Expose a repository as an IQueryable
- Fetch distinct record from datatable using linq to datatable and shows all columns of datatable
- Check if string value is a word
- Select multiple column in linq
- Linq to SQL how to do "where [column] in (list of values)"
- Linq Order By With Different Method
- Create XML using LINQ-to-XML API
- LINQ GroupBy on count
- Asp.Net MVC C# Cannot Implicitly Convert Type Issue