Monday, July 6, 2009

LINQ: How to use SQL's Not In query in LINQ

Querying data using "Not In" operator is very common for any programmer who involved in database layer of any application.

"Not In" operator is generally used to fetch all records/data in a table whose field(column) value does not match the given list of values. Assume you have Employee Table and it has a field called city, where the cities added are "A","B","C","D" and "E". Now if you want to fetch all the employees who are not living in cities "B", "C" and "E", in SQL you write query as below

Select * from employee where city not in ("B","C","E").

We can do the same in LINQ using "Except(List listObject)" method.

Generally we will use in-memory objects between different layers in our applications and in certain times we need to fetch a group of objects to implement a specific functionalities in the business model. Infact i had a list of 100 or more objects and i want to fetch a random of 10 objects based on a condition.

I explored different options like having a Hashtable or dictionary object and i'm not interested to create one collection just for this sake.
When i was googling for a solution i came to know about this method available in .Net 3.5 that simply solves my filter criteria without using foreach inside foreach....

Well here is the code which is similar to "Not In" operator in SQL.
Create a List of dummy data...

IList dataList = new List();
dataList.Add("1");
dataList.Add("2");
dataList.Add("3");
dataList.Add("4");
dataList.Add("5");
dataList.Add("6");

The below code fetch the top 2 records from the list and puts it into a new List

var topList = (from data in dataList
select data).Take(2);

Now to select a records/objects that does not match the toplist(data), simply we need to write the below code.

var filteredList = dataList.Except(topList);
The Except method will create a new list from dataList by excluding the records/data matching the topList.
Now filteredList will have only records/data that of "3,4,5 and 6"

This method really helps me in a situation where i dont know the filter criteria at compile time, size of the list, size of the filter list. On counting performance, this way is far better comparing to foreach





LINQ: How to use SQL TOP query in LINQ

I was looking for this particular feature in LINQ from the time i started using it in .Net 3.0, but not able to find a direct API, but now with .Net 3.5, it looks very simple.

When i found it for my new application a week back i was really happy mainly to avoid the workaround it requires to achieve the same.

Assume you have a List of objects or data as below

IList dataList = new List();
dataList.Add("14");
dataList.Add("22");
dataList.Add("35");
dataList.Add("4");
dataList.Add("10");
dataList.Add("60");

Now if you want to select top 2 records, u have to use Take(int count) method at the end of your LINQ query as listed below.

var topList = (from data in dataList
select data).Take(2);

This method takes any integer value and returns as many records available within the count.
Luckily i didn't get any exception when i gave 20 as a input to this method, for my analysis purpose:)

Well, for me this method should have taken unsigned int, instead of int type as i find no meaning to have int type

Sunday, July 5, 2009

Roger Federer won Wimbledon 2009

Oh my god, i would say the best finals i ever saw... Nerves were coming out and the heart beat kept increasing...... Finally at the end FedEx won against a Gun named Andy Roddick that throws everything best in him to stop Roger Federer surpassing Pete Sampras record of 14 Grand Slams...

My Hearty Congratulations to Mr Roger Federer... and i can sleep tonight peacefully after my favorite star's win...

But it was Andy Roddick who stolen my heart for his fight to taste the big victory... At one point of time i started supporting Andy for his tough fight, for his excellent service and placements... and i believe this man is definitely going to make history in the future. When i saw tears in his eyes, i really felt very bad for his loss and i wanted to wish him that he will achieve next time as Roger Federer did this time. Who knows this guy might beat FedEx records too...

Yes, last year Wimbledon, everyone saw tears in FedEx eyes as he lost to Nadal, may be that is forgettable now, but the great man came back, stormed into the finals and conquered the cup and he rightly deserved for that.

My Congratulations to FedEx again.

One Small Obligation to Federer : Please let others also to win soon:)

Finally i'm looking for a new champion who can break Federer's record....