score:6
What are you asking for is not natively supported, so there is no easier way, but for sure there is more efficient way because your current code is performing N + 1 database queries.
A better way could be to retrieve employees and related filtered payments with one database query using anonymous type projection, and then do something similar to your approach to create the final result in memory. For instance:
var listing =
db.Employees.Select(employee => new
{
employee,
payments = employee.Payments.Where(p => p.IsPosted == 1)
})
.AsEnumerable() // Switch to LINQ to Objects
.Select(r =>
{
r.employee.Payments = r.payments.ToList();
return r.employee;
})
.ToList();
score:0
Try something like this: It will give you a list of anonymous type that will hold the employee and it's payments.
using (dbcontext ctx = new dbcontext())
{
ctx.Connection.Open();
var result = (from e in ctx.Employees
join p in ctx.Payments on e.employeeId equals p.employeeId
where p.IsPosted == 1
select new
{
Employee = e,
Payments = p
}).ToList();
ctx.Connection.Close();
}
score:1
it colud be a good alternative
var listing = from d in db.Payments
.include("Employees")
.where d.IsPosted == 1
select d.Employees;
(not tested, then please fix mistakes)
start from pyaments, filter for is posted=1 , then select related emplyees
Source: stackoverflow.com
Related Query
- C# linq query filter child Collection
- Filter Parent Collection Using Criteria in Child Collection via a Linq Query
- LINQ - filter child collection
- How to use LINQ to filter property of child collection using (.Any())
- How to filter child collection with linq dynamic
- How do I query a collection of objects in LINQ by a child property?
- LINQ to XML: filter a query using XElement.Attributes() collection with both XName and Value
- Filter Linq Child Collection by another List/Array
- Using Lambda / Linq Filter Parent Collection based on Child Items
- Linq Using Child Collection in Query
- How to return a child collection with joins linq query
- How to Filter Child Collection from each Parent Entity in the Parent Collection using Linq
- Fluent NHibernate filter child collection with Linq
- Filter on child collection using LINQ
- LINQ query take(count) from collection of child elements
- Entity Framework LINQ Query match all members of child collection
- LINQ to Entities - sorting on child collection with filter
- Linq query is not populating Child Collection
- How to filter LINQ query based on child table column?
- LINQ Query to dynamically filter database where child table column equals optional input value
- Linq filter parent objects based on child objects collection
- LINQ Query - Only get Order and MAX Date from Child Collection
- Filter parent/child table (one to many association) in linq query based on entities in child table?
- C# Linq filter and remove record on child collection while returning parent
- Linq query to fill ViewModel collection including child ViewModel collections
- Linq how to query a list of items for a combined list of a child collection based on a property of the parent item
- Linq query to group by field1, count field2 and filter by count between values of joined collection
- Linq query that results in one field in parent entity being a collection of child entities
- Include Entities to Child Collection With Filter Linq EF5
- How Filter Binding Source Connected To linq Query
More Query from same tag
- How to do Multiple Left Join, Group By, and Concatenate in LINQ
- Get TOP5 records from each status using Linq C#
- union in two linq statements and remove the duplicate
- Group by and count in C# w/ Console.WriteLine
- Find item from the list
- Selecting ListBox values against a List
- Equals() is insensitive to letter cases in LINQ
- Searching in text files for a keyword until a string is encountered
- Return child nodes under different parents who have same name
- How to include fields that are not the Key or aggregate in Linq Group by results?
- c# create method that accept lambda for list of field name
- Check if words in string array ends and starts with char and insert new string between them (c#)
- Query rows that the datatable field contain any item in the list<string>
- How to write a Query to get sum total of each order in the list joining 3 tables
- Counting entries in a datagrid?
- VB.Net Lambda query
- Linq to XML and XPath nested descendants
- List order by another contained list
- Concatenating the values of one column into a single row
- Linq to sql to return related data
- LINQ to Entities - create median value in grouped data
- How to convert type 'byte[]' to 'System.Data.Linq.Binary'
- Linq to select parent records without children
- "The LINQ expression node type 'Invoke' is not supported in LINQ to Entities" - stumped!
- Mix Any() and First() in LINQ?
- LINQ syntax vs SQL syntax
- Can't update after Join added to query
- When does Entity framework Linq query Hits Sql Databse?
- Are LINQ to Entites 4.0 Queries Compiled By Default?
- Parallel search in 2 collection