Pages

Tuesday, September 13, 2011

My first day with LINQ! :)

Today I got to know about LINQ - Language Integration Query, in the same way Marie Curie found Radium (I think I can remember the correct story..May be not her!)

I had two arrays in my hand and I wanted to compare them and see whether they are equel. But I can't use the object's Equels method as it compares the memory location rather than  the value as Array is a reference type.

I could easily write a for-loop to achieve this but I wanted something new other than the old-fashioned for-loop...

There, I found LINQ!!! (Well.. I know I did not invent that, but Radium is also something that already existed rite? :P)

I could use LINQ join to achieve this. Here's the query I used:

private static bool CompareArrayOrder(string[] arr1, string[] arr2){

var q = from a in arr1
join b in arr2 on a equals b
select a;
return(arr1.Length == arr2.Length && q.Count() == arr1.Length);}

LINQ is something that you can map data with your objects such as arrays.

So, why use LINQ instead of for loop?

For the time being, what I know is, it ROCKS and COOLER than using for loops!
Other than that it makes your code simpler and less cumbersome thus making it easier to read.

No comments:

Post a Comment