Here the two matching elements are TOuter which is our Person table ( p) & TInner which is Emailaddress table (e). Func - A function to create a result element from two matching elements.In our case is BusinessEntityID field from EmailAddresses table. This is similar to the outerKeySelector, but the specify the key to be used from the second table. F unc innerKeySelector - A function to extract the join key from each element of the second sequence.The Key BusinessEntityID of the Person table is used to Join the two tables Here we use lambda expression p => p.BusinessEntityID. Func outerKeySelector - A function to extract the join key from each element of the first sequence.Here we use EmailAddresses, Which we are going to join to the Person Table. IEnumerable inner - The sequence to join to the first sequence.IEnumerable outer - The first sequence to join.Last per in and execute your project, you will get Output as belowĪnother way to get same results is using JOIN in Lambda expressions, Join Lambda has syntax as shown below public static IEnumerable Join( Where p.FirstName = "KEN" // now select person where people's FirstName =KENĪnd Inside Index.cshtml code = "Home Page" On p.BusinessEntityID equals e.BusinessEntityID //implement join on rows where p.BusinessEntityID = e.BusinessEntityID Join e in db.EmailAddresses // implement join as e in EmailAddresses table Person = (from p in db.People // get person table as p Using (AdventureWorks2012Entities db = new AdventureWorks2012Entities()) Go to your HomeController.cs and inside your Index ActionMethod, create the join code using LINQ public ActionResult Index() The query looks very similar to the normal database SQL Join Queries. The following query Joins the Person table with EmailAddress table on BusinessEntityID Key. The Join operator uses the Equals Keyword to compare the specified properties. Step 3: Suppose we want to implement Join on two tables Person and EmailAddresses table using the join Query operator. Step 2: Now let's create the Entity Model, right click on your projects's "Models" folder -> select "Add" -> select "Ado.NET entity model" ->name it "AdventureWorks2012" -> Select "EF designer from database" & Click Next -> Select "New Connection" from next steps and then login into your database and connect to it, click OKĬlick "Next" -> Select "Tables", click "Finish", ADO.NET entity modal is added in your project and we are connect to database using ADO.NET and Entity framework. Step 1: Let's create a new project in Visual Studio by navigating to File->New-> Project-> Select Web from left-pane and ASP.NET Web-application from right-pane -> click "OK" and then select "MVC" template, Visual studio will generate mvc template files and folders. Before we proceed, in this article, I will be using AdentureWorks database, so If you want to try this at your local pc, you can download and restore AdventueWorks database. In this article, I have explained how you can implement inner join on two tables or more tables using entity framework (ef) join in ASP.NET MVC and C# linq and lambda expression.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |