C# 中的 LINQ 查询

发布于 2024-10-11 00:26:48 字数 95 浏览 2 评论 0原文

如果我们可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

糖果控 2024-10-18 00:26:49

我个人认为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?

只为守护你 2024-10-18 00:26:49

我们无法告诉您为什么需要 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.

冷了相思 2024-10-18 00:26:49

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.

梦归所梦 2024-10-18 00:26:49
  • 可组合性:允许您使用不同的代码路径动态组合查询,而无需诉诸复杂且脆弱的 SQL 字符串连接
  • 类型安全性:由 LINQ to SQL 或 Entity Framework 生成的类具有很强的安全性。类型化,因此在编译时强制执行类型检查。
  • 留在面向对象的世界:您使用面向对象的语言进行编程,为什么不以面向对象的方式访问数据呢?导航属性允许您获取客户的所有订单,而无需编写单个查询:cust1.Orders
  • 可读性:LINQ 使用查询理解或扩展方法语法,比将不匹配的 DSL(例如 SQL)嵌入到现有 .NET 代码中更具可读性。
  • 快速应用程序开发 (RAD):借助 L2S/EF 和 LINQ,您可以立即开始运行。您可以立即启动并运行与数据库连接的应用程序。
  • 抽象:使用单一工具 LINQ,您可以访问各种类型的数据,甚至跨多个域创建查询:各种数据库、对象、XML、34 个以上...
  • 数据库生成:如果您认为 O/R 映射器仅将数据库映射到对象,那么您就错了 - 它也可以以其他方式工作。 EF 可以根据您创建的类自动创建数据库。这可能是一个相当好的起点,甚至对于小型系统来说已经足够好了。
  • 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.
海拔太高太耀眼 2024-10-18 00:26:49

DataTable 为您提供了一个缓存
无需 DB 即可重新枚举
LINQ to SQL 结果时的往返
需要显式缓存
类似 ToList()/ToArray() 的东西。
除了 DataContext 中的身份缓存之外,
L2S代码更接近于枚举
数据读取器。虽然 DataReader 确实
不允许你重新枚举和
需要另一个 ExecuteReader,
重新枚举 L2S 查询的影响是
相同 – 到数据库的另一次往返。

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

LINQ to SQL 与 ADO.Net

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文