
LINQ TO ENTIITES IN EFCORE:-
This is used to interact with database through Entity Framework Core.
It is used to write query using Entity Framework core data model using C# syntax. These queries are translated into sql queries in database, and the results are
returned as objects of entity types defined in model.
Further during operations like Filtering,Sorting,grouping or joining using LINQ, the DBSet converts these operations into SQL queries, executes them on the database, and the return the results
as entity objects to your application.
Theses queries are strongly typed, so it provides compile time checking and intellisense support.
What is Projection in LINQ?:-
It is the process of transforming data returned by query in different form.This new form contains only the properties that we need.
The following are the projection operations are available:-
1.Select
2.SelectMany
SELECT OPERATOR?:-
Select operator uses IEnumerable collection.The select operator is used when you want to select single value from given collection.
By two ways you can use Select operator,
SELECT USING QUERY SYNTAX
SELECT USING METHOD SYNTAX
First we will see "Select Using Query Syntax",
Here Select operator is used in creating a query using query syntax.In a query, select operator is used to return the collection of custom class
or anonymous type that include properties as per the user needs.
For eg:- find buses that goes in specific route.
//bus details
public class Busdetails
{
public string busfrom{get;set;}
public string busto{get;set;}
public date journeydate{get;set;}
public string busname{get;set;}
public string boardingpoint{get;set;}
public string destinationpoint{get;set;}
}
static void Main()
{
List<Busdetails> buses = new List<Busdetails>()
{
new Busdetails(){busfrom="chennai",busto="tirunelveli",journeydate=new DateTime(2026,01,15),busname="TRANZKING",boardingpoint="koyembedu",destinationpoint="vannarpettai"},
new Busdetails(){busfrom="chennai",busto="nagercoil",journeydate=new DateTime(2026,01,15),busname="PALSNT",boardingpoint="koyembedu",destinationpoint="vadasery"},
new Busdetails(){busfrom="chennai",busto="nagercoil",journeydate=new DateTime(2026,01,15),busname="INTERCITY",boardingpoint="koyembedu",destinationpoint="thakkalai"},
new Busdetails(){busfrom="chennai",busto="tirunelveli",journeydate=new DateTime(2026,01,15),busname="MUTHUMADAN",boardingpoint="koyembedu",destinationpoint="udayarpatti"},
new Busdetails(){busfrom="chennai",busto="tirunelveli",journeydate=new DateTime(2026,01,15),busname="YBMTRAVELS",boardingpoint="koyembedu",destinationpoint="udayarpatti"},
new Busdetails(){busfrom="chennai",busto="nagercoil",journeydate=new DateTime(2026,01,15),busname="NEEMTRAVELS",boardingpoint="koyembedu",destinationpoint="panagudi"}
};
string pbusfrom = "chennai";
string pbusto = "nagercoil";
DateTime pfrjourneydate = new DateTime(2026, 01, 15);
DateTime ptojourneydate = new DateTime(2026, 01, 15);
Here is the query to find all buses goes to tirunelveli based on following search condition i.e busfrom,busto,journeydate.
//Using select in query syntax
var res = from b in buses
where b.busfrom == pbusfrom && b.busto == pbusto && b.journeydate >= pfrjourneydate && b.journeydate <= ptojourneydate
select b.busname;
foreach(var val in res)
{
console.writeline("Bus Name{0}",val)
}
}
OUTPUT:-
Next, we will see "Select Using Method Syntax",
public class Customer
{
public int custid{get;set;}
public string CustomerName{get;set;}
public int CustomerPhone{get;set;}
public string CustomerCountry{get;set;}
public string CustomerState{get;set;}
public string CustomerCity{get;set;}
public string CustomerAddress{get;set;}
}
static void Main()
{
List<customer> cust=new List<customer>(){
{
new Customer(){custid=1,customername='raj',customerphone=2934333,customercountry=india,customerstate=tamilnadu,customercity=chennai,customeraddress=siliconcity},
new Customer(){custid=2,customername='shiv',customerphone=2937333,customercountry=india,customerstate=tamilnadu,customercity=madurai,customeraddress=whalescity},
new Customer(){custid=3,customername='ganesh',customerphone=2938333,customercountry=india,customerstate=karnataka,customercity=bangalore,customeraddress=goldcity},
new Customer(){custid=4,customername='muruga',customerphone=2939333,customercountry=india,customerstate=karnataka,customercity=bangalore,customeraddress=silvercity},
new Customer(){custid=5,customername='kandhan',customerphone=2931333,customercountry=india,customerstate=karnataka,customercity=bangalore,customeraddress=bronzecity},
}
Here is the query to find customer name,
//Using select in Method syntax
var res= cust.select(c=>new {id=c.custid,customername=c.customername});
foreach(var val in res)
{
console.writeline("customerid{0} customername{1}",val.custid,val.customername)
}
}
Searching Using LINQ to Entities in Entity Framework Core?
searching in database involves finding specific records that match given search criteria.For example,if we want only customers specific to tamilnadu
let us do search customers specific to tamilnadu,
public class AppDbContext :DbContext
{
public string ConnectionString{get;}
public DBSet<Customer> Customers{get;set;}
public AppDbContext(){
ConnectionString="Data Source=(localdb)\\MSSqlLocalDB;Initial Catalog=EFCoreSampleDB;IntegratedSecurity=true"
}
public override onConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConnectionString)
}
}
class program{
static void Main(string[] args){
try{
using (var context=new AppDbcontext())
{
string searchbystate='tamilnadu'
var result=(from cust in context.customers
where cust.state=searchbystate
select cust).ToList();
}
}
}
}