开发者

LINQ Queries in C# [closed]

As it currently stands, this 开发者_运维知识库question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Why we need LINQ in C# , if we can do anything using ADO.net. Then what is the need of LINQ queries? Are they optimized enough than ADO.net? Which is best one to use?


Personally I think that Linq greatest strength is that it optimizes the developer. It's beauty lies in freeing the developer query over variety of data sources using the same query language extensions.

You can answer how many classes are in the System.Web namespace or how many customers in our database are in Ottawa using virtually the same syntax.

Of course that comes at a cost but isn't that what optimization means?


We can't tell you why you need LINQ. That's for you to decide. Also there is a difference between LINQ and what you perceive as LINQ. LINQ does not solely mean querying SQL. LINQ is built into the compiler to translate query statements into method calls, and there are many providers that you can use with LINQ, Linq-to-Sql is one of them (I believe this is what you are referring to), Entity Framework is another, Linq-to-Objects is another, Linq-to-Xml etc.

Linq-to-Sql (and EF) are ORM frameworks used to map database objects from your database domain to your application domain. ORM frameworks can greatly reduce development time whilst providing automatic data loading, property mapping, relationship aware models, etc.

You can do all of this with vanilla ADO.NET too, but you have to roll your own. My question to you would be, is it feasible for you to use an ORM framework in your project when you consider a) your existing codebase, b) development deadlines, c) maintainability.


Linq to Sql (which I assume is what you are referring to), is useful because it provides a convenient abstraction.

It is far simpler to write a linq query than to write sql, and then hydrate an object manually.


  • Composability: allows you to compose queries dynamically using different code paths without resorting to complicated and fragile SQL string concatenation
  • Type Safety: The classes generated by LINQ to SQL or Entity Framework are strongly typed, and so type checks are enforced at compile time.
  • Staying in the Object Oriented world: you program in an object oriented language, why not access data in an object oriented fashion? Navigation properties allow you to get all the orders of a customer without writing a single query: cust1.Orders.
  • Readability: LINQ, using query comprehension or extension method syntax, is much more readable then embedding a mismatched DSL such as SQL into existing .NET code.
  • Rapid Application Development (RAD): With L2S/EF and LINQ, you can hit the ground running. You can get a database-connected application up and running in no-time.
  • Abstraction: Using a single tool, LINQ, you can access various types of data, and even create a query across multiple domains: Various databases, objects, XML, 34 more...
  • Database Generation: if you thought an O/R mapper only maps a database to objects, you thought wrong - it can work the other way too. EF can automatically create a database based on the classes you've created. That can be a reasonably good starting point, or even good enough for small systems.


DataTable provides you a cache that can be re-enumerated without DB roundtrip while LINQ to SQL results need to be explicitly cached with something like a ToList()/ToArray(). Identity caching in DataContext aside, the L2S code is closer to enumerating a DataReader. Although DataReader does not allow you to re-enumerate and requires another ExecuteReader, the impact of reenumerating L2S query is the same – another roundtrip to DB.

http://blogs.msdn.com/b/wriju/archive/2008/07/14/linq-to-sql-vs-ado-net-a-comparison.aspx

LINQ to SQL vs ADO.Net

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜