C# 中的 LINQ 查询
如果我们可以使用 ADO.net 做任何事情,为什么我们需要 C# 中的 LINQ。那么什么情况下需要LINQ查询呢? 它们是否比 ADO.net 优化得足够好?哪一个最好用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人认为Linq最大的优势在于它优化了开发人员。它的美妙之处在于让开发人员可以使用相同的查询语言扩展来自由地查询各种数据源。
您可以使用几乎相同的语法来回答 System.Web 命名空间中有多少个类,或者我们的数据库中有多少个客户位于渥太华。
当然这是有代价的,但这不就是优化的意义吗?
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?
我们无法告诉您为什么需要 LINQ。这由你决定。 LINQ 和您所理解的 LINQ 之间也有区别。 LINQ 不仅仅意味着查询 SQL。 LINQ 内置于编译器中,用于将查询语句转换为方法调用,并且有许多提供程序可以与 LINQ 一起使用,Linq-to-Sql 就是其中之一(我相信这就是您所指的),Entity Framework 是Linq
-to-Sql(和 EF)是 ORM 框架,用于将数据库对象从数据库域映射到应用程序域。 ORM 框架可以大大减少开发时间,同时提供自动数据加载、属性映射、关系感知模型等。
您也可以使用普通的 ADO.NET 来完成所有这些工作,但您必须自己动手。我的问题是,当您考虑 a) 现有代码库、b) 开发期限、c) 可维护性时,在项目中使用 ORM 框架是否可行。
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(我认为这就是您所指的)非常有用,因为它提供了方便的抽象。
编写 linq 查询比编写 sql 简单得多,然后手动水合对象。
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.
cust1.Orders
。cust1.Orders
.http://blogs.msdn.com/b/wriju/archive/2008/07/14/linq-to-sql-vs-ado-net-a-comparison.aspx
LINQ to SQL 与 ADO.Net
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