Wednesday 17 July 2013

Differentiate between 'All' and 'Any' Operators in LINQ.

In LINQ, All operator is  to check whether all the elements met the specific condition or not. While Any operator checks whether there is any element exist in the collection or not.


For Example:

int array={1,2,3,4,5};

bool result=array.All(i=>i < 6); here it will check the elements and returns true.

Similarly,

bool result= array.Any( ); // here it checks whether the collection has any elements and returns true.


Hope you Enjoyed!

Monday 15 July 2013

Compile Time View Checking In Asp.net MVC

You might have noticed that in MVC3/MVC4 if you had done some thing wrong in the view files it will not throw any error during compilation and it will build successfully. The error in the file is only noticed during executing/rendering the view file. So in order to trap the error during the compilation time you can follow the method below:

Step-1 :
Open your solution project file in any XML Editor or in the Visual Studio itself  and you will find a tag named
<MVCBuildViews>, here it false and just make it true and save it.

<MVCBuildViews>false</MVCBuildViews>  
 change it to 
<MVCBuildViews>true</MVCBuildViews>


Thats all, so now when your view has any error then it will throw error during the compilation.


The Use of ALL Operator in LINQ

Today, I came across the All operator in LINQ. This operator returns bool value rather than records after condition is satisfied. The operator will be highly helpful when you need some kind of validation to find unique data. for e.g. Checking the existing email  or UserID registered and so on.


Lets Go with a Example to do the Task:

int [] array = { 1,2,3,4,5 };

bool result = array.All(value => value > 2 );

Console.WriteLine(result);  // Here it will print False since it will check all the values and                                         // all the values must be grater than 2.


 result = array.All(value => value < 6 );
Console.WriteLine(result); // Here it will print True since it will check all the values and                                          // found all values are less than 6.


Hope it helps you!