ASP.NET - LINQ

Asked By myzonal.com myzonal.com
04-Nov-11 02:27 AM
Hi All

Why we use LINQ and what is diffrence between LINQ and SQL?
  Suchit shah replied to myzonal.com myzonal.com
04-Nov-11 02:36 AM

LINQ which I think is one of the most exciting features in Orcas. LINQ makes the concept of querying a first-class programming concept in .NET. The data to be queried can take the form of XML (LINQ to XML), databases (LINQ-enabled ADO.NET: LINQ to SQL, LINQ to Dataset and LINQ to Entities) and objects (LINQ to Objects). LINQ is also highly extensible and allows you to build custom LINQ enabled data providers (e.g.: LINQ to Amazon, LINQ to NHibernate, LINQ to LDAP).

LINQ Architecture (MSDN Magazine - June 2007)

I will discuss some of the new language features and improvements that are introduced in C# 3.0, and it is those features that enable the full power of LINQ which makes it possible to write something like this:



LINQ is a uniform programming model for any kind of data access. LINQ enables you to query and manipulate data independently of data sources. Below figure 'LINQ' shows how .NET language stands over LINQ programming model and works in a uniformed manner over any kind of data source. It’s like a query language which can query any data source and any transform. LINQ also provides full type safety and compile time checking.
LINQ can serve as a good entity for middle tier. So it will sit in between the UI and data access layer.
 

Figure - LINQ

 

Below is a simple sample of LINQ. We have a collection of data ‘objcountries’ to which LINQ will is making a query with country name ‘India’. The collection ‘objcountries’ can be any data source dataset, datareader, XML etc. Below figure ‘LINQ code snippet’ shows how the ‘ObjCountries’ can be any can of data. We then query for the ‘CountryCode’ and loop through the same.

Figure: - LINQ code snippet

How does LINQ help us from the perspective of business objects?
 

One of the tedious jobs in business object is parsing and searching object collections. For instance consider the below figure where we want to search a country by an ‘ID’ value. So what we do is loop through the collection and get the object. Many may argue how about keeping a key in List or Array. The below example is just a sample. For instance if you want to search using country code and name, list / collection keys will not work with those multi-value searches.
 

In other words using LINQ we can query business object collections and filter the collection in a single LINQ query.


  1. LINQ syntax beats SQL syntax. SQL is flawed in that queries become exponentially difficult to write as their complexity grows. LINQ scales much better in this regard. Once you get used to it, it's hard to go back.
  2. Database queries are easily composable. You can conditionally add an ORDER BY or WHERE predicate without discovering at run-time that a certain string combination generates a syntax error.
  3. More bugs are picked up at compile-time.
  4. Parameterization is automatic and type-safe.
  5. LINQ queries can directly populate an object hierarchy.
  6. LINQ to SQL provides a model for provider independence that might really work.
  7. LINQ significantly cuts plumbing code and clutter. Without sweeping stuff under the carpet, like Workflow or Datasets. This is a credit to the design team.
  8. C# hasn't suffered in the process (in fact, it's gained).

  James H replied to myzonal.com myzonal.com
04-Nov-11 04:32 AM

LINQ by itself is just a base technology ("Language Integrated Query") that's baked into C# 3.0 - it has nothing to do per se with databases. LINQ can be used against a variety of things - databases, XML, objects in memory, Entity Framework entities, Active Directory - you name it.

Linq-To-SQL is the lightweight, straightforward, MS-SQLServer only technology which allows you to use SQL Server tables easily and nicely as real objects in your .NET application. It's a "Object-relational mapper" that makes dealing with databases easier. It's SQL Server only, and Microsoft will not extend it much further - it's available, also in .NET 4.0, but won't be further developed any more.

ADO.NET is the base data access technology in .NET - it gives you access to a wide variety of data stores, relational and non-relational. It's the very basic technology - you deal with your data in a very low-level, raw manner.

On top of that, you have the ADO.NET datasets, which are a bit like Linq-to-SQL in that they make it somewhat easier to deal with databases. Contrary to Linq-to-SQL, you're not dealing with your objects from your domain model in your .NET code, but instead you're dealing with the database-oriented rows and columns just like they exist in the database. It's a more direct representation of what's in the database, it's on a lower level, it's very tightly coupled to your database layout, and it's not as "nice" and easy to use as Linq-To-SQL objects - you deal with lowlevel rows and columns and their values.

If you have the choice right now, and don't need anything but SQL Server, I would highly recommend checking out Linq-to-SQL - the mapping from the raw database tables to nice and easy to use .NET objects really makes your life a whole lot easier!


Check out http://www.dotcominfoway.com/blog/linq-to-sql-vs-ado-net-entity-framework for a quick comparison. For me linq to sql is a quicker way to get a small application up and running and the entity framework is more for large more complex applications that you can take time to build a big foundation for.
  kalpana aparnathi replied to myzonal.com myzonal.com
04-Nov-11 06:22 AM
Visit this link for introduction to LINQ:

http://odetocode.com/Articles/737.aspx

And differance between LINQ & SQL visit this link:

http://stackoverflow.com/questions/1081916/difference-between-linq-to-sql-linq-typed-datasets-ado-net
  R B replied to myzonal.com myzonal.com
04-Nov-11 07:53 AM
Hello,

SQL  : sql query queries only database
LINQ :: linq helps to query on collections,sql classes,objects collections etc


Hope this  helps
Create New Account
help
hi what is difference between linq and ado.net. . .can u plz give the idea with examples ADO.NET is the underlying data access API for .NET Framework (much like JDBC in Java). It's been around since the first release of .NET. LINQ to SQL is a data access framework built on ADO.NET and new language features that makes SQL Server data available natively in the object
value = "17" > ASP.NET< / option > < option value = "18" > WCF / WF< / option > < option value = "10" > ADO / ADO.NET< / option > < option value = "99" > WebMatrix< / option > < option value = "95" > Entity Framework< / option > < option value = "83" > RIA Services< / option > < option value = "56" > Silverlight, WPF, and XAML< / option option value = "53" > GDI+< / option > < option value = "63" > LINQ< / option > < option value = "16" > Compact Framework< / option > < option value = "7" > Visual Studio .NET< / option > < option value = "71" > NHibernate< / option > < option value = "41" > Deployment< / option > < option value = "44" > Security< / option > < option value = "47" > Architecture value = "61" > Windows 7< / option > < option value = "15" > Windows Vista< / option > < option value = "11" > Windows Server< / option > < option value = "87" > Linux / Unix OS< / option > < option value = "85" > MAC OS< / option > < option value = "91" > Windows Update< / option > < / optgroup > < optgroup label = "Server Applications" > < option value = "69" > SharePoint< / option > < option value = "51" > IIS< / option > < option value = "70" > BizTalk
how to select Topic C# .NET F# .NET VB.NET ASP.NET WCF / WF ADO / ADO.NET WebMatrix Entity Framework RIA Services Silverlight, WPF, and XAML GDI+ LINQ Compact Framework Visual Studio .NET NHibernate Deployment Security Architecture Windows XP Windows 7 Windows Vista Windows Server Linux / Unix OS MAC OS Windows Update SharePoint IIS BizTalk Exchange Server Site Server Subversion / CVS Excel Word InfoPath Powerpoint Access Publisher Money Outlook Windows Live JavaScript
Last Name Rohman Topic C# .NET F# .NET VB.NET ASP.NET WCF / WF ADO / ADO.NET WebMatrix Entity Framework RIA Services Silverlight, WPF, and XAML GDI+ LINQ Compact Framework Visual Studio .NET NHibernate Deployment Security Architecture Windows XP Windows 7 Windows Vista Windows Server Linux / Unix OS MAC OS Windows Update SharePoint IIS BizTalk Exchange Server Site Server Subversion / CVS Excel Word InfoPath Powerpoint Access Publisher Money Outlook Windows Live JavaScript