我在LINQPad中做了一个快速测试,并且惊喜地发现多次检索具有相同数据上下文的同一行返回相同的对象引用:
var p1 = Products.Single(x => x.ProductID == 1);
var p2 = Products.Single(x => x.ProductID == 1); // this does not query
ReferenceEquals(p1, p2).Dump(); // True
// Run more complicated query, and dig through result rows to get the same row.
var result =
(from d in OrderDetails
select new { OrderDetail = d, Product = d.Product }
).ToArray();
var p3 = result.Select(r => r.Product).First(x => x.ProductID == 1);
ReferenceEquals(p1, p3).Dump(); // True
这是保证的行为吗? LINQ-to-SQL是否始终是它检索的实习对象,或者是否可以让后续查询为相同的数据返回不同的对象?