Using Expression in C#

C# Logo

When working with an ORM like Entity-Framework this can be incredibly useful for building dynamic queries from an entity in a function. It allows you to pass a function as a parameter and have that function be executed on the entity. I need to credit a fellow student who I had the privilege of working with at Penn State on my Capstone project for this one.

The benefit of this is that it is type specific so when writing code you will have intellisense available. Additionally, you can create extremely dynamic queries with a few lines of code. This example assumes the use of Entity Framework Core, although it can be adapted to nearly any ORM relatively easily.

Repository Setup:

Usage Example:

This example will return all users who have a bio that is not null. You could of course tie this into any custom element on the repository you wanted.

This could of course get far my complex, but my hope was that this would introduce you to the basics. With enough interest I will explore this in more detail.

23 views0 comments

Recent Posts

See All

©2020 by Justin Wilson