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
Over 2 million developers have joined DZone.
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

  • Implementing Observability in Distributed Systems Using OpenTelemetry
  • 5 Common Security Pitfalls in Serverless Architectures
  • Chaos Engineering Has a Blind Spot. Agentic AI Lives in It.
  • Stateless JWT Auth Microservice Architecture With Spring Boot 3 and Redis Sentinel
  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 user avatar
Muhammad Adil Malik
·
Feb. 18, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
24.8K Views

Join the DZone community and get the full member experience.

Join For Free

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

The likes didn't load as expected. Please refresh the page and try again.

  • 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