Introduction
In this article we will learn what is LINQ. What is the difference between Select and where in linq. Example of Linq Where and Select.
Previous Updates
In previous articles we have learnt Why every business needed digital Marketing ,What is Lock and how to achieve lock on sql table. Authorization in Asp.Net. How high quality content affects your Website. What is Blocking and Deadlock In SQL. Transaction Commit and Rollback in sql server with example.
In this article we will learn what is LINQ. What is the difference between Select and where in linq. Example of Linq Where and Select.
Previous Updates
In previous articles we have learnt Why every business needed digital Marketing ,What is Lock and how to achieve lock on sql table. Authorization in Asp.Net. How high quality content affects your Website. What is Blocking and Deadlock In SQL. Transaction Commit and Rollback in sql server with example.
Select and Where are two completely different operators acting on IEnumerables
Select : (IEnumerable<T1>, Func<T1,T2>) -
Select in Linq takes Ienumerable type of input T1 and Func (function) produce a boolean result for an in
In other words Select is a projection, so what you get is the expression x=> x.Name== "JP" evaluated for each element in MyList() on the server. i.e. lots of true/false values (the same number as your original list). If you look the select will return something like IEnumerable<bool> (because the type of x=> x.Name == "John" is a bool).
var a = MyList.SELECT(x => x.Id == id).SingleOrDefault();
|
public static IEnumerable<TResult> Select<TSource , TResult>
( this IEnumerable<TSource> a , Func<TSource , TResult> Method )
{ foreach ( var item in a ) { //Each iteration call the delegate and return the Data back. // (use 'yield' for deferred return) yield return Method.Invoke ( item ); } } |
Related topics - Difference between Ienumerable and Iqueryable. LINQ overview , Advantanges and Lamda expression in LINQ.
Where : (IEnumerable<T1>, Func<T1, bool>)
Where in Linq takes Ienumerable type of input T1 and Func (function) transferring element of Type T1 into element of Type T2.
var a = MyList.Where(x => x.Id == id).SingleOrDefault();
|
public static IEnumerable<Tsource> Where<Tsource>
( this IEnumerable<Tsource> a , Func<Tsource , bool> Method )
{
foreach ( var data in a )
{
//If the lambda Expression(delegate) returns "true" Then return the Data.
//(use 'yield' for deferred return)
if ( Method.Invoke ( data ) )
{
yield return data;
}
}
}
|
0 comments:
Post a Comment