[Entity Framework Core] How to specify columns with variables in a Where statement [ASP.NET Core]

Hello, this is Hase from the System Development Department

I recently started developing with ASP.NET Core again, and
column names using variables when retrieving
specify

Introduction

First, let's say you have table data like this:

Accounts

Id NickName Last Name FirstName
1 Rena Hatakeyama Reina
2 Toshiaki Matsuzaki Shunmyo
3 Sena Ishii Sena
4 Shozo Kokura Shozo
5 Kotaro south Kotaro

*This is test data andthiswas extracted from

I would like to extract records from this data by specifying the columns and values

The usual way

In the normal way, you cannot specify columns dynamically, so the code will look like this:

string columnName = "LastName"; // Column name string value = "Hatayama"; // Search value // Extract records from the specified column name and value var query = context.Accounts; switch (columnName) { // When the NickName column is specified case "NickName": query = query.Where(c => c.NickName== value); break; // When the LastName column is specified case "LastName": query = query.Where(c => c.LastName== value); break; // When the FirstName column is specified case "FirstName": query = query.Where(c => c.FirstName == value); break; } var account = query.FirstOrDefault();

This results in a very redundant code, where each column name is compared with a case in the switch statement

Specifying column names using variables

However, Entity Framework Core has a way to dynamically specify column names!
That'sthe EF.Property methodcalled
This is a great tool that allows you to dynamically specify properties (column names) as arguments.

If you actually modify the previous code to use this method,

string columnName = "FirstName"; // Column name string value = "Sena"; // Search value // Extract records from the specified column name and value var account = context.Accounts.Where(c => EF.Property<string> (c, columnName) == value).FirstOrDefault();

And it can now be written in just one line without using a switch statement.
*You need to use Microsoft.Entity Framework Core.

summary

What did you think?

Personally, I found this to be a very useful feature.
I think it will be especially helpful when you want to implement a rich search function, so
please keep it in mind when using Entity Framework Core!

lastly

I have launched "SEKARAKU Lab," a service site for the system development company I belong to.
Beyond offers a one-stop service for everything from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.
SEKARAKU Lab:[https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)

It's short, but that's all

If you found this article helpful,please give it a "Like"!
4
Loading...
4 votes, average: 1.00 / 14
5,383
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Tatsuya Hase

Joined Beyond Co., Ltd. as a new graduate

We develop web systems (development of browser-based services and systems such as web services, digital content, and business management systems) and game APIs (development of programs for communication with app games)

We also develop private/custom apps for Shopify

Originally worked at the Osaka office, but transferred to the Yokohama office in 2019.
Hobbies: baseball, karaoke, anime.