Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1019789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:04:04+00:00 2026-05-16T11:04:04+00:00

This is for Entity Framework for .NET 3.5: I have the need to query

  • 0

This is for Entity Framework for .NET 3.5:

I have the need to query a table and include a collection of the “many” table of a one-to-many relationship. I’m trying to filter that collection as part of the query – I’m pretty new to Entity Framework, and I’m having trouble figuring it out.

Simplified example: Author has Books, and Book has an IsFiction column. I want a filtered list of authors, along with all fiction books.

Without the filter, it’s easy:

var q = from a in db.Authors.Include("Books")
        where a.BirthYear > 1900
        select a;

I can filter after the fact, something like:

var fictionBooks = a.Books.Where(b => b.IsFiction);

But the problem is that the original query already ran, and included those results, which is unnecessary database processing.

I can query separately, like:

var q = from a in db.Authors where a.BirthYear > 1900 select a;
foreach (var a in q)
{
    var books = from b in db.Books 
                where ((b.Author.Id == a.Id) && (b.IsFiction))
                select b;
}

But of course that’s one call for every author, which I want to avoid as well.

I can go backwards, like:

var allBooks = from b in db.Books.Include("Author")
               where b.IsFiction
               select b;

But then I’m back to the original problem, except now on the author side instead of the book side.

There must be a solution that encompasses everything – I can do it in SQL pretty easily:

select * from author a
left join book b on a.id = b.author_id and b.is_fiction = 1
where a.birth_year > 1900

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:04:04+00:00Added an answer on May 16, 2026 at 11:04 am

    The forward way:

    var q = from a in db.Authors.Include("Books")
            where a.BirthYear > 1900
            select new {
                Author = a,
                FictionBooks = a.Books.Where(b => b.IsFiction)
            };
    

    Another way, derived from the SQL at the bottom of your question:

    var q = from a in db.Authors
            from b in db.Books.Include("Author")
            where a.BirthYear > 1900 && b.IsFiction && a.Id == b.Author.Id
            select new { Author = a, Book = b };
    

    The main difference between these two is that the first one will basically give you a collection of authors plus the list of fiction books for each author (which may be empty); while the second will give you a collection of author/books pairs (so it doesn’t return any authors with no fiction books).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning ADO.NET entity framework. This is my first example. The datagrid seems
Using Entity Framework, I suddenly get this strange error after publishing my asp.net mvc
I'm currently using Entity Framework and am running into this issue: The relationship between
I am using entity framework and I can't find include method like in this
We have a WCF service that uses Entity Framework to query a SQL database.
Using ASP.NET MVC and Entity Framework, I encounter some attach/detach errors when I need
I have an ADO.NET Data Service that exposes an Entity Framework data model (.edmx).
We have an Entity Framework project with several models set up using .NET 4
I have read a bit about ADO.NET Entity Framework, but there is some things
I need to have an entity that has two separate candidate keys where one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.