DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From Naked Objects to Naked Functions
  • Hyperlambda, the Fastest Programming Language in the World
  • Working With Transactions in Entity Framework Core and Entity Developer
  • Working With Lazy Loading and Eager Loading in Entity Framework Core and Entity Developer

Trending

  • Part 1: Building Governed MCP Tool Services With Quarkus LangChain4j and Goose
  • To Switch, or Not to Switch the AI Model
  • How You Clear Your HTML5 Canvas Matters
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Include With Where Clause

Include With Where Clause

In this article, take a look at a tutorial that explains how to write a certain query in Entity Framework.

By 
Muhammad Adil Malik
·
Feb. 18, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Ever thought about writing the following query in Entity Framework?

SQL
 




xxxxxxxxxx
1


 
1
Select * From Employees e
2
left join Employees_Attendance ea on e.id=ea.employee_id and ea.date between {{startdate}} and {{enddate}}



In the above query, {{startdate}} and {{enddate}} are the parameters. This query will list all of the employees, but only the attendance that are between startdate and enddate. 

While working on a project, it seems very hard to do this using the standard EF Core library, so I found an online solution available I thought worth sharing.

A library has a lot of useful functionalities, including the one we are trying to solve, that can be downloaded from https://entityframework-plus.net/download.

While using that library's extension method IncludeFilter, the above query can be written in EF Core as follows:

C#
 




xxxxxxxxxx
1


 
1
_dbContext.Employees
2
    .IncludeFilter(x => x.EmployeeAttendance.Where(y => y.Date >= {{StartDate}} && y.Date <= {{EndDate}}))



StartDate and EndDate are the parameters that can be replaced by the parameter in your solutions.

Thanks for reading. Happy coding!

Further Reading

Consider Using [NOT] EXISTS Instead of [NOT] IN

Entity Framework Database

Opinions expressed by DZone contributors are their own.

Related

  • From Naked Objects to Naked Functions
  • Hyperlambda, the Fastest Programming Language in the World
  • Working With Transactions in Entity Framework Core and Entity Developer
  • Working With Lazy Loading and Eager Loading in Entity Framework Core and Entity Developer

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook