Join Entities in EF Core & Search in EF Core – Session 53

Join Entities & Search in EF Core - Session 53-min

During this session, we explain Join Entities in EF Core and talk about Search in EF Core and work with lambda expressions (LINQ) for this purpose. Also, we implement these features on the ASP.NET Core Web App that we developed until now. Based on the added entities to the Web App that we developed for this tutorial, Now we can create a proper join view with them to deliver a better user experience to the end-user. Also, we will add the Search feature to the Web App and show you the way that you can use LINQ.

Join Entities in EF Core

For Joining entities, we can use the Join method and then connect the related properties of the entities. As you can see in the below figure, Category ID is displayed in the list instead of Category Name which is not a proper option in terms of user experience.

List before Join Entities

To solve the mentioned issue, we need to Join the Cost entity which just has CategoryID, and the Category entity which has both CategortID and CategoryName. Then, we can select the properties that we need for the View.

To implement this feature on the Web App, first, we need a ViewModel with the selected properties.

Then, we need to add a new Method to the ICostRepository interface.

Next, we implement the declared method in the respective repository or repositories.

As shown above, the entities have been joined with the Join method. Moreover, we use lambda expressions to connect the respective properties. Now, we can use this method to have a list of Costs that includes CategoryName. Thus we use it in CostController/Index Action Method.

Also, we need to modify the Cost/Index View as well.

Hence, the result is the below figure that shows Category Name instead of Category ID.

Cost List after Joining the Entities

Search in EF Core

We can Search the list of matched records with the applied conditions in EF Core. For this purpose, we can use the Where method and the comparative operations same as ==, <, >, <=, >=, != and StratsWith, EndsWith, Equal, Contains methods. In this session, we try the == and Contains method and you can try the rest. Worth mentioning that, we can use Contains method when we are looking for records that contain the search phrase.

To implement this feature, we need to add a Search Form to our Cost/Index View.

The added form method is GET and the form address CostController and Index Action Method. Thus, the search phrases are passed to the Controller via Query String Parameters. So, we need to modify the Controller

Moreover, the Interface and the Repository should be changed to meet the application requirement.

we should apply the Interface changes to the respective repository.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes


0 0 votes
Article Rating

Inline Feedbacks
View all comments