Friday, August 6, 2010

LINQ --- An interesting Technology...

Hello Friends,
Linq is very interesting Technology introduced in .Net Framework 3.5.
LINQ stands for Language Integrated Query.
Over View Of LINQ:
  • LINQ provides query functionality directly in C# programming languages as we do in Sql Server
  • LINQ queries are language constructs which are similar to any other defined code block, such as methods and class definations.
  • We can run LINQ queries against any .NET Framework collection that implements IEnumerable, IEnumerable(T), or any collection that implements an interface that inherits from IEnumerable(T), blocks of XML and XML Documents, SQL Server databases, and ADO.NET datasets.
LINQ Providers:
There are mainly four types of LINQ Providers available:
  1. LINQ to Objects
  2. LINQ to XML
  3. LINQ to SQL
  4. LINQ to DataSets
Besides the above four, Other Providers is also supported.
LINQ Queries:
The LINQ query by itself defi nes only the shape and structure of the data returned by the
query, not the actual data. The data is available after executing the query. The process of
targeting a data source with a LINQ query and accessing the data has three stages:
1. The data source
2. Query creation
3. Query execution
The Data SourceThe data source is any .NET Framework collection that implements IEnumerable,
IEnumerable(T), or any collection that implements an interface that inherits from
IEnumerable(T), Blocks of XML and XML Documents, SQL Server databases, and ADO.NET
DataSets.
Query CreationQuery creation is the LINQ query that determines the data to retrieve from the data source.
In addition to the actual data to return, LINQ queries can also specify additional information,
such as sort order, and grouping of the returned data.
The following code shows a LINQ query that returns all buttons on a form:
var buttonsQuery =
from Control buttons in this.Controls
where (buttons is Button)
orderby buttons.Text
select buttons;
The variable that will contain the results of the query is called the range variable. In the
above example, the variable is named buttonsQuery.
Query ExecutionQuery execution is typically deferred until the query is iterated over with a foreach loop. In
the case of aggregate functions, such as Sum, Count, and Average, these queries execute
immediately and do not need to be iterated over.
To force immediate execution of a query, call the ToList or ToArray methods of the query’s
range variable, as shown in the following example.
var buttonsQuery =
from Control buttons in this.Controls
where (buttons is Button)
orderby buttons.Text
select buttons.ToList();
In the example LINQ query shown above, you force the query to execute immediately by
calling buttons.ToList().


Thanks,
Paras Sanghani

How to Bind Enums to windows Combobox?


Hello Friends,
Sometime our ComboBox lookup value are fixed and we don’t have any database table. In such situation we mostly make enum of our look up value. But most of the time we are used to bind datasource of tables or list to combbox. So we might be wonder how to bind our enum to combobox.

Here is the simple way to bind combobox. Say for example you have RoleTypes Enum.

public enum RoleTypes
{
Owner = 1,
Manager = 2,
AssitManager = 3,
Employee = 4
}
Now, to bind this enum to our combo box.

this.cmbUserRole.DataSource = Enum.GetValues(typeof(RoleTypes));

Set value to combobox.

this.cmbUserRole.SelectedItem = (RoleTypes)1;
To get value from combobox.

Convert.ToInt32((RoleTypes) this.cmbUserRole.SelectedItem);


Moreover, kindly have a look at the enum and how to create the same :


Enumerations are related symbols that have fixed values. Use enumerations to provide a list of choices for developers using your class. Moreover, it is way to avoid the hardcode values in the applicaiton. For example, the following enumeration contains a set of titles:


// C#
enum Titles : int { Mr, Ms, Mrs, Dr };


If you create an instance of the Titles type, Visual Studio displays a list of the available values when you assign a value to the variable. Although the value of the variable is an integer, it is easy to output the name of the symbol rather than its value, as shown here:


// C#
Titles t = Titles.Dr;
Console.WriteLine("{0}.", t); // Displays "Dr."

The purpose of enumerations is to simplify coding and improve code readability by enabling you to use meaningful symbols instead of simple numeric values. Use enumerations when developers consuming your types must choose from a limited set of choices for a value.

Thanks,

Paras Sanghani