Tuesday, October 5, 2010

DIFFERENCE BETWEEN FINDALL() AND WHERE()

First of all how both these works :
Suppose we have a class Person :

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

Now, create an object of person class :
Person person = new Person();

Take a List of person class :
List lstPerson = new List();

Now, assign values to this class and add it to list :
person.FirstName = "FirstName1";
person.LastName = "LastName1";
lstPerson.Add(person);

person.FirstName = "FirstName2";
person.LastName = "LastName2";
lstPerson.Add(person);

Now, I want to find all the records where FirstName = "FirstName1".
To do this, I have two options : FindAll() and Where()

FindAll() :
List result = lstPerson.FindAll(delegate(Person per)
{
return per.FirstName == "Firstname1";
});

Where() :
List result = lstPerson.Where((Person per) => per.FirstName == "FirstName1").ToList();


Both will return the same result. So, the question is which one should be used in which situation?
There are some difference between both, now you have to decide which one suits your requirement:

1. FindAll() is a function on the List type, it's not a LINQ extension method like Where. The LINQ extension methods work on any type that implements IEnumerable, whereas FindAll can only be used on List instances (or instances of classes that inherit from it, of course). If our collection is IEnumerable type, then we can't use FindAll() as it's a function of List.

2. The FindAll method of the List class actually constructs a new list object, and adds results to it. The Where extension method for IEnumerable will simply iterate over an existing list and yield an enumeration of the matching results without creating or adding anything (other than the enumerator itself.)

3. Where is much faster than FindAll. No matter how big the list is, Where takes exactly the same amount of time because Where() just creates a query, It doesn't actually do anything, unlike FindAll which does create a list.

Read More..

Friday, June 4, 2010

FIND() AND FINDALL() FUNCTIONS IN LINQ

We have a table Customer which contains details of a customer like name, address, phone, etc.

we have a list of customers -
List lstCust = new List();

we want to find a particular record from this list based on some criteria.
say where name = 'abc';

In C# :

List lstCustResult = lstCust .FindAll(
delegate(Customers cust)
{
return cust.name == "abc";
});

This will return all the records having name ="abc".

suppose we want to fetch a particular value based on some criteria, in that case we will use Find instead of FindAll.

string name = lstCust .Find(
delegate(Customers cust)
{
return cust.id== 1;
}).name;

Also, if suppose we have a list of strings and want to find a particular record in that list.
For That we need to do something like this :

List lstNumbers = new List();
lstNumbers.Add("One");
lstNumbers.Add("Two");
lstNumbers.Add("Three");
lstNumbers.Add("Four");
lstNumbers.Add("One");
List result= lst.FindAll(delegate(string name)
{
name = "One";
return lstNumbers.Equals(name);
});
Read More..

Friday, January 1, 2010

LINQ to SQL

LINQ to SQL is a component of .NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects.

In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.

By using LINQ to SQL, you can use the LINQ technology to access SQL databases just as you would access an in-memory collection.

For example, the nw object in the following code is created to represent the Northwind database, the Customers table is targeted, the rows are filtered for Customers from London, and a string for CompanyName is selected for retrieval.


When the loop is executed, the collection of CompanyName values is retrieved.


Read More..

Monday, December 21, 2009

Introduction to LINQ

Language-Integrated Query (LINQ) is a groundbreaking innovation in Visual Studio 2008 and the .NET Framework version 3.5 that bridges the gap between the world of objects and the world of data.

Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. LINQ makes a query a first-class language construct in C# and Visual Basic. You write queries against strongly typed collections of objects by using language keywords and familiar operators.

The following illustration shows a partially-completed LINQ query against a SQL Server database in C# with full type checking and IntelliSense support.

LINQ query with Intellisense

Read More..