Updated February 15, 2023
Introduction to Entity Framework LINQ
Entity Framework LINQ is a modern Object Database Mapper for .NET Applications. LINQ to Entity supports the Language-Integrated Query, allowing developers to code the queries in the EF Conceptual Model using the VB or Visual C#. Entity Framework supports the LINQ queries, which modify the tracking, schema migrations, and updates. Entity Framework engaged with various databases are SQL Database, MySQL, SQLite, Azure Cosmos DB, and PostgreSQL.
Overview of Entity Framework LINQ
Entity Framework and LINQ recommend efficient benefits for the .NET developers. Entity Framework provides the services of Object-Relational Mapping on the data models. The LINQ (Language Integrated Query) defines the set operators to project and filter data in arrays, XML, enumerable classes, and other data sources. There is one form of LINQ, and LINQ to Entities enables querying the Entity Framework data sources. The framework called ODP.NET supports the EF like Oracle Database, which contributes to Object-Relational-Modelling and LINQ to Entities queries.
It summarizes the database data model from the applications data model. When functioning with Object-Relational data, it’s easier with the EF tools.
Let’s consider a few points as follows:
- The LINQ to Entities and Entity Framework maintains in ODP.NET for the .NET Framework Version-4. ODP.NET for the .NET Framework Version-2.0 does not support LINQ to Entities and ADO.NET Entity Framework.
- The Code-First approach supports Entity Framework Version-6 and higher versions.
- To bind the scalar parameters is supported with the ODP.NET and the Entity Framework. The EF is supported by the parameter binding by the name and not by binding by position.
The LINQ to Entities can be achieved through the queries on the Oracle Database by using the ODP.NET, which includes the LINQ to Entities built-in functions. Furthermore, the operations like INSERT, DELETE, and UPDATE can be implemented by using the Oracle Stored Procedures, or it is done by using the ObjectContext and SaveChanges methods.
Entity Framework LINQ Extension Methods
In Entity Framework LINQ Extension methods, the LINQ offers standardized query operators like Grouping, Filtering, Projection, Aggregation, Concatenations, Sorting, and so on. In addition, it contains several operators to accomplish various types of functionality; they are called extension methods in LINQ.
The LINQ Extension methods cover several operators as follows:
1. Project Operator
Project operators contain Select and SelectMany.
Let’s see both operators briefly:
- Select: The Select Operator used to retrieve the data from the collection will be configured as per the requirement in the resultant set.
- SelectMany: SelectMany is used to retrieve the data from Collections of collection.
Look at the following code, which is used to retrieve the data from the collection (Select Operator):
a. Projection Operator (Select)
Code:
var result = db.ProductMasters
.Where(p => p.ProductName.StartsWith("M"))
.Select(p =>
new
{
p.ProductId
p.ProductName
}).ToList();
b. Projection Operator (SelectMany)
Code:
Likewise for the SelectMany Operator,
var selectMany_Products = db.ProductMasters
.Where(p=> p.ProductName.StartsWith("C"))
.SelectMany(p => p.StoreMaster,(p,s) =>
new
{ p.ProductName,
s.StoreName,
}).ToList();
foreach (var item in selectMany_Products)
{
Console.WriteLine(item.ProductName+"\t"+item.StoreName);
}
2. Grouping Operator
The Grouping Operator in LINQ implements the IGrouping<TKey, TSource> interface. The TKey specifies the key value, and the list of values specified by the TSource matches the given grouping key.
The GroupBy returns the group of values based on the value of the group key. For example, let’s see the following sample code, which describes the data from two tables, the ProductMaster and the StoreMaster it lists the Products belonging to the same stores/ shop, so it returns the list of products based on the stores /shop.
Code:
var resultGroupBy = from p in db.ProductMasters
join s in db.StoreMasters
on p.StoreId equals s.StoreId
into productGroup
select new
{
Products = p.ProductName,
Stores = p.StoreMaster.StoreName
};
foreach (var storeGroup in resultGroupBy)
{
Console.WriteLine(storeGroup.Stores);
foreach (var item in storeGroup.Products)
{
Console.WriteLine("-" + item);
}
}
The LookUp does a similar process same as GroupBy. The difference between GroupBy and LookUp is that the execution process is immediate in LookUp, and note that LookUp is valid only through method syntax, not in query syntax.
Code:
var getLookUp = db.ProductMasters.ToLookup(p => p.StoreId);
foreach (var group in getLookUp)
{
Console.WriteLine(group.Key);
foreach (var item in group)
{
Console.WriteLine("Product Name : " + item.ProductName);
}
}
Entity Framework LINQ Filter
Different filters are mentioned below:
Filtering Operator: Filtering Operator retrieves the elements based on specific conditions with filtering collections.
There are two filtering operators used to filter the collections; they are as follows:
1. Where
Let’s see with one example by using the Where clause with the Stores ID, so it filters the list of items based on the stores and finally returns the elements.
Example:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_EF_LINQ
{
class Program
{
static void Main(string[] args)
{
using (dbContextCon db = new dbContextCon())
{
/* var resWh = from p in db.ProductMasters
where p.StoreId == 2
select p.ProductName; */
Console.WriteLine("Where Operator");
Console.WriteLine("--------------");
var res = db.ProductMasters.Where(p => p.StoreId == 2).Select(p => p.ProductName);
foreach (var item in res)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}
}
2. OfType
The OfType Filter Collections it is based on particular types.
Let us see with one example we have used the OfType<String> from this; we can get the entire string type elements from the collections.
Example:
Code:
Var resOfType = ListItem.OfType<string>();
Examples of Entity Framework LINQ
Let’s see the simple example for the Select operator; it is a LINQ query containing the two table lists one is Product Master, and another one is Stores Master; both tables include the properties of the ProductMaster table (like productID,productName, productDescription and so on) and StoresMaster table (contains StoresID, StoreName, StoreContactNumber and so on) respectively. To retrieve the values from the database tables, look at the sample program below.
The Select is used to retrieve the data from the collection of items. SelectMany operator is used to retrieving the data from collections of collection.
Example #1: Projection Operator (Select)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_EF_LINQ
{
class Program
{
static void Main(string[] args)
{
using (dbContext db = new dbContext())
{
Console.WriteLine("Select Operator");
Console.WriteLine("---------------\n");
//var getProduct = from p in db.ProductMasters select p;
var prod = db.ProductMasters
.Where(p => p.ProductName.StartsWith("C"))
.Select(p =>
new
{
p.ProductName,
p.ProductDescription,
p.ProductId
}).ToList();
foreach (var select_Result in prod)
{
Console.WriteLine("\nProduct ID:"+select_Result.ProductId+"\tProduct Name:" + select_Result.ProductName + "\t\tDescritpion:" + select_Result.ProductDescription);
}
Console.ReadLine();
}
}
}
}
Output:
Example #2: Grouping Operator (LookUp)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Console_EF_LINQ
{
class Program
{
static void Main(string[] args)
{
using (con_Db db = new con_Db ())
{
Console.WriteLine("ToLookup Operator");
Console.WriteLine("-----------------");
var getLookUp = db.ProductMasters.ToLookup(p => p.StoreId);
foreach (var group in getLookUp)
{
Console.WriteLine(group.Key);
foreach (var item in group)
{
Console.WriteLine("Product Name : " + item.ProductName);
}
}
Console.ReadLine();
}
}
}
}
Output:
Conclusion
This article has seen about the Entity Framework LINQ; it contains the various extension methods for grouping, projecting, and so on.
Recommended Articles
This is a guide to Entity Framework LINQ. Here we discuss the introduction, entity framework LINQ extension methods, and examples. You may also have a look at the following articles to learn more –