Wednesday, May 21, 2008

Windows Power Shell

Windows PowerShell

Windows PowerShell is advanced and extended command line interface shell and also it provides feature of scription language. It is available for following OS

* Windows XP SP2
* Windows Server 2003
* Windows Vista
* Built into Windows Server 2008 as optional feature

Also this shell is integrated with .NET Framework. So we can class and objects of .NET in our scripts. Windows PowerShell also provide Hosting Mechanism so it means you can embedded this Shell into your application and utilise the features and power of the PowerShell.



Visit
------
http://www.microsoft.com/technet/scriptcenter/topics/winpsh/pshell2.mspx
http://www.microsoft.com/downloads/details.aspx?FamilyId=7C8051C2-9BFC-4C81-859D-0864979FA403&displaylang=en
http://blogs.msdn.com/PowerShell/

Sunday, May 11, 2008

Relations(Joins) in LINQ

Following is the sample which shows how can you use relations in the LINQ for query and get the data out of various objects


class Customer
{
public int CustomerID;
public string CustomerName;
}



class Order
{
public int OrderID;
public int CustomerID;
public DateTime OrderDate;
}



class CustomerOrders
{
public IEnumerable GetCustomerOrders()
{
Customer[] customers = {
new Customer{ CustomerID =1, CustomerName = "Pratap Singh"},
new Customer{ CustomerID =2, CustomerName = "Sikandar"},
new Customer{ CustomerID =3, CustomerName = "Ram Parsad Patnaik"}
};

Order[] orders = {
new Order{ CustomerID = 1, OrderID = 1 , OrderDate = Convert.ToDateTime("10/Apr/2004")},
new Order{ CustomerID = 1, OrderID = 2 , OrderDate = Convert.ToDateTime("3/Jan/2005")},
new Order{ CustomerID = 3, OrderID = 1 , OrderDate = Convert.ToDateTime("23/Mar/2006")},
new Order{ CustomerID = 2, OrderID = 1 , OrderDate = Convert.ToDateTime("14/Nov/2005")},
new Order{ CustomerID = 3, OrderID = 2 , OrderDate = Convert.ToDateTime("19/Dec/2008")}
};

var Query = from clist in customers
join oList in orders
on clist.CustomerID equals oList.CustomerID
select clist;

return Query;
}

}


Here in sample look at the join part of the LINQ join oList in orders on clist.CustomerID equals oList.CustomerID. In smiliar way you can create various joins and create a relational model in LINQ

Saturday, May 10, 2008

Local type inference

Local type inference is a language feature that allows you to define variables and use them without worrying about their true type. Local type inference is also interchangeably known as implicitly typed local variables. The burden is put on the respective language compiler to determine the type of a variable by inferring it from the expression assigned to the variable. The result is type safety while allowing you to write more relaxed code, which is required to support Language Integrated Query (LINQ).

Type inference can only be used within a local scope where its type can be inferred by the expression assignment. Type inference cannot be applied to any of the following:

* Cannot be a part of a member property declaration on a class, struct, or interface
* Cannot be used in a parameter list on a method
* Cannot be a return type for a method
* Cannot be defined without a right hand assignment expression
* Cannot reassign to be a different type once type has been inferred


namespace Sample.TypeInference
{
class Program
{
static void Main(string[] args)
{
int a = 5;
var b = a; // int
var x = 5.5M; // double
var s = "string"; // string
var l = s.Length; // int

Console.WriteLine("value of b is {0} and type is {1}",
b, b.GetType().ToString());
Console.WriteLine("type of x is {0}", x.GetType().ToString());
Console.WriteLine("type of s is {0}", s.GetType().ToString());
Console.WriteLine("type of l is {0}", l.GetType().ToString());

Console.ReadLine();
}
}
}

Sample of LINQ

In the following sample we will search for the customer having greater than the specified amount and will show there name in the message box

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace SampleLinqApplication
{
public partial class CustomerDisplay : Form
{
public CustomerDisplay()
{
InitializeComponent();
}

private void findCustomer_Click(object sender, EventArgs e)
{
LinqSample sample = new LinqSample();
sample.FindCustomers();
}
}

class Customer
{
public string Name;
public string City;
public double Amount;
}

class LinqSample
{
public void FindCustomers()
{
Customer[] customers = {
new Customer { Name = "Anirudh", Amount = 9000, City = "Gurgaon" },
new Customer { Name = "Omkar Nath", Amount = 12202, City = "Nasik"},
new Customer { Name = "Peter", Amount = 123000, City = "Washington DC" },
new Customer { Name = "Madhav", Amount = 65553, City = "Bhopal"},
new Customer { Name = "Manpreet Singh", Amount = 789445, City = "Ludhiana"},
new Customer { Name = "Chin Yung", Amount = 5689, City = "Shanghai"}
};

var Query = from c in customers
where c.Amount > 10000
orderby c.Name
select c;

foreach (Customer cust in Query)
{
MessageBox.Show(cust.Name);
}
}
}
}

What is linq ?

Linq is a programing model which is introduced as new concept in the .Net Languages. It is also known as Language Integrated Query. Linq requires some of the extension for the complete support. These extension boosts the productivity also it shortens and gives meaning to syntax for better manipulation


Following is the linq query sample which returns the elements of array in ascending order and are greater then 5
var query = from v in AValues
where v > 5
orderby v ascending
select v;

Now we can retrieve the values from v with the help of the foreach loop in following manner

foreach(int vItem in v)
{
MessageBox.Show(vItem.ToString());
}

now the code of abive query will be converted by the compiler as the following

IEnumerable Query = AValues.Where(v => v > 5)
.OrderBy(v => v).Select(v => v);