Linq 包含在 MVC 应用程序中
我有一个带有连接的 SQL 查询。我想使用 Linq。有人建议我避免连接并使用 Linq include。如何使用 Linq include 组合实体?还有比 join 更好的方法吗?
谢谢
I have an SQL query with joins. I want to use Linq. some one suggested me to avoid joins and use Linq include. How can I combine entities using Linq include? Is there a better way than joins ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
LINQ to SQL 将 LINQ 语法转换为 SQL 查询。如果查询需要联接,则 LINQ to SQL 实际上将针对数据库执行带有联接的 SQL 查询。您可能获得的优势是 LINQ to SQL 查询可能比您编写的查询更加优化。
如果您不熟悉 LINQ 的使用,并且您有一个可用的应用程序,那么通过添加额外的抽象层 (LINQ),您可能不会获得除了复杂性之外的任何好处。话虽如此,LINQ 是一个优秀而强大的工具。然而,这并没有改变这句格言的真理:“如果它没有坏,就不要修理它。”
基本上,您正在考虑的是将应用程序的数据访问过程从 转换
为
我非常精通这两种过程,并且我认为编写 LINQ 查询比编写 SQL 查询容易得多。 (您可以获得强类型模型和智能感知)。但是,如果您只了解 SQL,那么编写 SQL 查询比学习 LINQ 和编写 LINQ 查询要容易得多。
请注意,LINQ 还支持联接。这是一篇有关在 LINQ 中使用联接的文章:http://odetocode.com/Blogs/scott/archive/2008/03/25/inner-outer-lets-all-join-together-with-linq.aspx 。
LINQ to SQL converts LINQ syntax to an SQL query. If the query requires a Join, then LINQ to SQL will actually execute an SQL query with joins against the database. The advantage you may gain is that the LINQ to SQL query may be more optimized than the one you have written.
If you are not familiar with using LINQ and you have a working application, you will probably not gain anything other than complication by adding an additional layer of abstraction (LINQ). That being said, LINQ is an excellent and powerful tool. However, that doesn't change the truth to the adage "If it ain't broke don't fix it."
Basically, what you are considering is converting your application's data access procedure from
to
I am pretty well versed in both, and I think writing LINQ queries is a lot easier than writing SQL queries. (You get strongly typed models and Intellisense). However, if you only know SQL, then writing an SQL query is significantly easier than learning LINQ and writing a LINQ query .
Note that LINQ also supports joins. Here is an article on using joins in LINQ: http://odetocode.com/Blogs/scott/archive/2008/03/25/inner-outer-lets-all-join-together-with-linq.aspx .