两者之间有什么区别

1)

context.connection.open()

var albums = (from a in context.Albums
              where a.id == id
             select a);

context.connection.close()


2)

context.connection.open()

var albums = (from a in context.Albums
             select a);

context.connection.close()

var result = albums.where((a)=>{a.id == id});


使用第一个会更快吗

最佳答案

第二句中的where子句看起来是不正确的语法,除了打开/关闭连接外,它们还应评估为相同的代码。也就是说,当实际枚举结果集时将生成并执行SQL。

您可以省略其余代码,而只需编写:

var albums = from a in context.Albums
             where a.id == id
             select a;


要么

var albums = context.Albums.Where(a => a.id == id);


枚举结果时,他们将对相同的事物求值。

关于c# - linq与sql where子句有何不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1678643/

10-15 13:48