LINQ to Object和LINQ to SQL查询之间的差异 [英] Differences between LINQ to Objects and LINQ to SQL queries

查看:445
本文介绍了LINQ to Object和LINQ to SQL查询之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在使用LINQ查询我的POCO对象,但是我还没有尝试过LINQ to SQL.我假设LINQ to SQL查询以某种方式转换为等效的SQL查询,并且鉴于此,我想知道这是否会影响LINQ to SQL查询的编写方式或应该如何编写.

I have been using LINQ to query my POCO objects for some time, but I have not yet tried LINQ to SQL. I assume that LINQ to SQL queries are somehow converted to equivalent SQL queries and, given this, I am wondering if that affects the way LINQ to SQL queries are or should be written.

LINQ to Objects和LINQ to SQL之间是否有任何显着差异会影响我应该如何编写查询?

Are there any significant differences between LINQ to Objects and LINQ to SQL that affect how I should write a query for either?

推荐答案

主要区别在于您所说的LINQ to SQL查询被转换为SQL.这意味着您可以编写实际上无法转换的代码,或者具有一些微妙的语义的代码,而您只能在执行时才能发现它们.

The main difference is as you say, LINQ to SQL queries are converted into SQL. That means that there is code you can write which isn't actually convertible or has some subtly different semantics - and you only find that out at execution time.

例如:

var query = from person in people
            where person.Age == person.GetHashCode()
            select person;

可以编译,但是在执行时失败,因为LINQ to SQL不知道如何使用GetHashCode().

will compile fine, but fail at execution time because LINQ to SQL doesn't know what to do with GetHashCode().

基本上,我发现LINQ to SQL比LINQ to Object更难预测.这并不是说它没有用-只是一个稍微不同的世界. MS在让您编写经常可以满足您期望的查询的事情上做得非常出色.

Basically I find LINQ to SQL a lot harder to predict than LINQ to Objects. That's not to say it's not useful - it's just a slightly different world. MS has done an amazing job at letting you write queries which very often just do what you expect them to, but it can't do everything.

这篇关于LINQ to Object和LINQ to SQL查询之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆