LINQ - Interview Questions - Asked By Kaviya on 24-Jun-11 06:03 AM

Hi..
   Any One Say Me what is LINQ y we r using it in .NET,What the purpose of using LINQ i need to prepare interview pl say me.
Bala Marish replied to Kaviya on 24-Jun-11 06:29 AM

LINQ is one of the new entrants in C# 3.0, introduced as part of .NET 3.5. As you are already aware, LINQ is a unified querying framework to retrieve data from various data sources such as relational databases, XML, plain objects and new LINQ providers are being written for other types of data sources as well.

LINQ to DataSets

.NET 3.5 comes with many LINQ providers out-of-the-box and one of them is LINQ to DataSets. Even though ADO.NET provides mechanisms in the form of methods and properties, LINQ to DataSets provides a easy and flexible option that resembles standard SQL in terms of language syntax to query and filter data in DataTable objects. Let us see an example of querying a DataTable using LINQ:

C#

DataTable dt = CustomerDataHelper.GetCustomers ();

var nycCustomer = from dr in dt.AsEnumerable ()
                  where (dr.Field<string> ("City") == "New York") && (dr.Field<int>("CustomerID") > 100)
                  select new
                  {
                      CustID = dr.Field<int> ("CustomerID"),
                      CustName = dr.Field<string> ("CustomerName")
                  };
foreach (var c in nycCustomer)
{
    Console.WriteLine (String.Format("{0} - {1}", c.CustID, c.CustName));
}

One thing to notice from the above code is the way we access individual columns in the DataRow: we use Field<>(), a generic extension method of the DataRow class. Field acts as a wrapper around the DataRow columns to provide type-safe access to the underlying column values (remember, one of the advantages of LINQ is type-safe querying) and it provides a few other benefits as well. LINQ can also be used on typed DataTable instances. In this case, we do not have to use the Field extension method to access the columns, because the typed DataTable itself takes care of column type-safety.

Of course, the same query can be written using standard query operators (method invocation syntax) also. For example, the following all-method-invocation syntax is equivalent to the above SQL- like syntax and produces identical results. In fact, SQL-like syntax is converted to the standard query operator syntax behind the scenes during compilation.

Riley K replied to Kaviya on 24-Jun-11 08:43 AM
LINQ has got more advantages when you want to query a COLLECTIONS,
where it has got many methods for convenience.

When dealing with database though you get good features like intellisence it is comparitively slow with ADO.net,

When connected with SQL Database you can get schema of the database and query on tables with intellisence.
jason rle replied to Kaviya on 28-Jun-11 11:10 PM
I don't think you can get more details here. Your question is basic and abstract, you should learn it systematically to acquire what you want.
You'd better refer the MSDN document from http://msdn.microsoft.com/en-us/library/bb308959.aspx, and then, you will learn more from MSDN.
Kirtan Patel replied to Kaviya on 04-Jul-11 09:53 PM
What is LINQ
---------------
If some one wants to develop database application on .Net platform the very simple approach he uses ADO.Net. ADO.Net is serving as middle ware in application and provides complete object oriented wrapper around the database SQL. Developing application in C# and VB.Net so developer must have good knowledge of object oriented concept as well as SQL, so it means developer must be familiar with both technologies to develop an application. If here I can say SQL statements are become part of the C# and VB.Net code so it’s not mistaken in form of LINQ. 

Why to use LINQ 
---------------------
Linq queries are all strongly typed since classes are autogenerated to match your database schema. As a result, you also get intellisense all the way through composing a query. For example, in the above query, after typing "c." you would get an intellisense popup showing the properties of the Customer class, just like regular coding in Visual Studio. This also means that if you change the name of a table or column in the database, you no longer have to hunt through all your stored procedures to change references to the old name, instead you can just follow the trail of build errors in your code. You can even be tricky by doing name refactor from the old name to the new name before updating the schema.

Thanks