获取LINQ以预加载完整的表 [英] Get LINQ to preload a complete table

查看:70
本文介绍了获取LINQ以预加载完整的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要LINQ来获取整个表,但这似乎不起作用...每次我通过pkey选择值时,都会再次触发选择..

所以,实际上这段代码:

        DataContext dc = new DataContext();

        dc.Stores.ToList();

        Store st = dc.Stores.SingleOrDefault(p => p.Id == 124671);

正在制作

select * from store 

使用"ToList()"方法和其他

select * from store where id = 124671

在其下方的选择部分...

当然,我想阻止它进行第二选择.

我该怎么办? (我不想将ToList()的结果存储在诸如List< Store>的其他属性中)

更新:

关于您的答案,这意味着:

Store st = stores.SingleOrDefault(p => p.Id == 124671);
Store st = stores.SingleOrDefault(p => p.Id == 124671);

触发器2是否会选择DB,这将使LINQ-idea失效?!还是我在这里出了什么问题?

我认为LINQ基本上会保存我在选择中捕获的所有数据,并且仅当在缓存"中找不到数据时才执行另一个请求.因此,我认为它就像介于两者之间的某种魔术"存储层一样.我的应用程序和数据库.

更新#2

只是您知道这个主意..我想在一开始就失去性能(抓取所有数据时),而当我从缓存的"数据中进行选择时,要赢回它...

解决方案

(我假设它知道id是主键,等等)

哪个完全版本?看起来像LINQ-to-SQL.在3.5(无Service Pack)中,是-身份管理器是位垃圾.它具有在3.5 SP1中更好,并且据信已在4.0中修复.

在.NET 3.5 SP1中,Single(predicate)方法可用于IIRC,所以也许使用它并捕获块? try/catch将比网络跃点更快.

在以后的连接文章中:

此错误现已修复,将在 包含在.NET Framework 4.0中.这 优化以首先搜索缓存 现在将完成基于ID的查找 为了 Single/SingleOrDefault/First/FirstOrDefault(谓词) 也 Where(谓词).Single/SingleOrDefault/First/FirstOrDefault(), 谓词具有相同的地方 像以前一样限制.

I need LINQ to grab a whole table, but this seems not to be working... everytime i select on the values via the pkey, a select is fired again..

So, actually this code:

        DataContext dc = new DataContext();

        dc.Stores.ToList();

        Store st = dc.Stores.SingleOrDefault(p => p.Id == 124671);

is making a

select * from store 

at the "ToList()" method and an ADDITIONAL

select * from store where id = 124671

at the selection part below it...

Of course, i want to prevent it to make the second select..

How would i do it? (I DON'T want to store the ToList() result in an additional property like List< Store > )

UPDATE:

Regarding your answers that would mean, that:

Store st = stores.SingleOrDefault(p => p.Id == 124671);
Store st = stores.SingleOrDefault(p => p.Id == 124671);

would trigger 2 selects to the DB, which would make the LINQ-idea useless?! Or what am i getting wrong here?

I thought LINQ would basically save all the data i grabbed in the selects and ONLY performs another request when the data was not found in the "cache".. So, i thought of it like some kind of "magical" storagelayer between my application and the database..

UPDATE #2

Just that you get the idea.. i want to lose the performance at the beginning ( when grabbing all data ) and win it back alter when i select from the "cached" data...

解决方案

(I'm assuming it knows that the id is the primary key etc)

Which exact version? That looks like LINQ-to-SQL; in 3.5 (no service packs), yes - the identity manager was a bit rubbish at this. It got better in 3.5 SP1, and is supposedly fixed in 4.0.

In .NET 3.5 SP1, the Single(predicate) approach works IIRC, so perhaps use that and catch block? A try/catch will be faster than a network hop.

From the later connect post:

This bug has now been fixed and will be included in .NET Framework 4.0. The optimization to search the cache first for ID-based lookups will now be done for Single/SingleOrDefault/First/FirstOrDefault(predicate) as well as Where(predicate).Single/SingleOrDefault/First/FirstOrDefault(), where predicate has the same restrictions as before.

这篇关于获取LINQ以预加载完整的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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