Monday, September 15, 2008

What is LINQ?

One of the few things I had to use more frequently in last few days is LINQ. Although LINQ is not a new topic I had to use it more frequently so I thought to write something about LINQ including my experiences in it.

It's not been so long since Microsoft has included typed-dataset in Visual Studio 2005 as their latest information access method. It seems that the next big challenge in programming is to reduce the complexity and time of information access. If you want to develop a database application using .net simplest approach was to use ADO.Net. ADO.Net works as the middle ware of your application. It provides a complete object oriented wrapper around the database. To develop a database application using ADO.Net you have to have a good knowledge in SQL as well as programming concepts. Visual Studio 2008 comes with the general purpose query facilities to .net framework which is applicable to various information sources not just relational data which is called Language-Integrated Query (LINQ).

LINQ provides a set of general purpose standard query operators to traverse, filter, order, etc... to facilitate those operations. It helps any .Net based programming language. These standard query operators are applicable to any kind of information source which implements the IEnumerable interface.

LINQ query/operators

string[] names = { "Ajantha", "Gayan", "Jennifer", "Leo",
"Nalaka", "Rasika", "Rajitha", "Randy" };

IEnumerable<string> query = from q in names
where q.Length == 6
orderby q
select q.ToUpper();

foreach (string name in query)

{
Response.Write(name + " ");
}

if you compile and run this code you'll see the output,
NALAKA RASIKA

There are 3 standard query operators used in this query expression. where, orderby, select. This query expression can be re-written as,

IEnumerable<string> query = names
.Where(s => s.Length == 6)
.OrderBy(s => s)
.Select(s => s.ToUpper());

This type of a query is called a method based query. Arguments passed to Where, OrderBy, Select operators are called lambda expressions which are piece of codes or much like delegates. It allows to write individual query operators and bind together with the dot(.).

Friday, May 30, 2008

DataTable.Rows.InsertAt(DataRow, Position)

I wanted to interchange row positions of some records in my data table. Actually I wanted to search some records and bring those records on top of the data table. So I wrote this piece of code.

DataRow[] foundRows = dt.Select("firstName+' '+lastName='" + SearchString + "'");

for (int i = 0; i <>

{
dt.Rows.Remove(foundRows[i]);
dt.Rows.InsertAt(foundRows[i],i);
}

I didn't see any wrong with this code so I debugged to test the code. what I was expected to get the selected records on top of the data table as first few rows but the output was not what I was expected. replaces first few columns showed as blank records. When inserting records through InsertAt method it has inserted blank columns. so I checked the data row (foundRows[i]) just before inserting to the data table through InsertAt method and it showed the values of the record properly.

Finally my conclusion was after Inserted the new record to the data table through InsertAt method, those records become blank records. So I changed the above code as,

DataRow[] foundRows = dt.Select("firstName+' '+lastName='" + SearchString + "'");

for (int i = 0; i < foundRows.Length; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = foundRows[i]["id"];
dr["FirstName"] = foundRows[i]["FirstName"];
dr["LastName"] = foundRows[i]["LastName"];
dr["Title"] = foundRows[i]["Title"];
dr["Location"] = foundRows[i]["Location"];
dt.Rows.Remove(foundRows[i]);
dt.Rows.InsertAt(dr,i);
}


And it worked perfectly. I had to create a new data row, assign the selected data row to that and insert newly created data row to the data table.

Wednesday, May 28, 2008

Fuel hike

Diesel prices increased from Rs.80 to Rs.130. It's a 62.5 percent increase from the current prices. This increase of diesel prices will affect many industries in sri lanka. Bus fair increased immediately from 27.2 percent and it might rise again in next couple of days. This will be the most touching increase of fuel hike to sri lankan citizens in the recent history.

Recent hike in food and fuel prices is not limited to sri lanka. Countries in war zones like Somalia, Afghanistan and Haiti are already suffering from this problem. Now it will start to feel to sri lankan citizens also with this fuel hike.

Most of the economists say that this will be a year of very high expenditure. I hope the sri lankan government will take necessary actions to help sri lankan citizens to balance their weight of expenditure.

Friday, April 04, 2008

undefined

if (obj == null)
What do we expect to check by using the above condition in javascript?

javascript never sets a value to null. So we can check only whether we have assigned null programmatically by using the above condition. In most of the programming languages if we want to check whether a values has been assigned to a variable or not we can use the above condition(C# as an example) but not in javascript. When a value is not assigned to a variable in javascript, it assigns the default value of 'undefined'. So check undefined.

if(typeof obj == 'undefined')

Tuesday, April 01, 2008

?? Operator

I have been addicted to use ? operator in C# for sometime. If I want to check a condition and do something according to the true/false status of that condition we used this method in old days.

if (Request.QueryString["param1"] != null
)
param1 = Request.QueryString["param1"];
else
param1 =
"";

After the introduction of the "?" operator I used to write the same piece of code as,
string param1 = Request.QueryString["param1"] != null ? Request.QueryString["param1"].ToString() : "";

C# has introduced another operator which is "??". It is used to replace null. So I'll kept in mind that to force myself to use "??" operator where I can use it as,

string param1 = Request.QueryString["param1"] ?? "";