LINQ to SQL执行 [英] LINQ to SQL execution
本文介绍了LINQ to SQL执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
请看这个片段:
Hi all,
Please look at this snippet:
var query = from t in db.ThingTable
where t.Id == id
select t;
if (!query.Any())
return null;
ThingTable result = _Query.First();
LINQ to SQL不是我的事,但我有一个问题。因为(在我的理解中)查询变成了一个由SQL Server执行的表达式树,所以查询将被执行两次是否有任何危险 - 一次是Any()而一次是First()?或者它是否足够聪明以重复使用结果集?
谢谢。
LINQ to SQL isn't really my thing, but I have a question. Because (in my understanding) the query gets turned into an expression tree to be executed by SQL Server, is there any danger that the query will get executed twice - once for the Any() and once for First()? Or is it smart enough to reuse the result set?
Thanks.
推荐答案
这将有效更快!
This will work way faster!
var foundThingTable = db.ThingTable.FirstOrDefault(tt => tt.Id == id);
return foundThingTable;
我不确定,如果重用结果集足够聪明,那就是我使用这样的querry的原因:
I am not sure, if it is enough smart to reuse the result set, that's why I am using such querry:
var query = (from t in db.ThingTable
where t.Id == id
select t).FirstOrDefault();
if (query == null)
return null;
ThingTable result = query;
在这种情况下,您将只获得一条所需的记录,而无需调用First()来获取第一条记录。
In this case you will get only one record that you need and no need to call First() to get first record.
这篇关于LINQ to SQL执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文