Get LINQ to preload the full table

I need LINQ to grab the whole table, but that doesn't seem to work ... every time I select values ​​with the pkey, the selection starts again.

So actually this code:

        DataContext dc = new DataContext();

        dc.Stores.ToList();

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

does

select * from store 

in the "ToList ()" method and OPTIONAL

select * from store where id = 124671

in terms of choice under it ...

Of course, I want him not to make a second choice.

How can I do it? (I DO NOT want to store the result of ToList () in an additional property, such as List <Store>)

UPDATE:

Regarding your answers, which mean that:

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

will run 2 choices in the database, which would make the LINQ idea useless ?! Or am I wrong here?

, LINQ , select, , "". - "" .

# 2

, , ( ) , "" ...

+3
4

( , , id ..)

? LINQ-to-SQL; 3.5 ( ), - . 3.5 SP1 4.0.

.NET 3.5 SP1 Single(predicate) IIRC, , , ? A try/catch .

:

​​ .NET Framework 4.0. /SingleOrDefault//FirstOrDefault () () /SingleOrDefault//FirstOrDefault(), -.

+4

:

DataContext dc = new DataContext();

var allStores = dc.Stores.ToList();

Store st = allStores.SingleOrDefault(p => p.Id == 124671);
+8

- ToList()

- ToList() . Linq , IQueryable.

, :

var theStore = dc.Stores.SingleOrDefault(p => p.Id == 124671);

, ToList() :

var allStores = dc.Stores.ToList()

,

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

- . , .Net , ..

0

, , , . .

The method ToListreturns a list as a result, which you can save in a variable and query:

var stores = dc.Stores.ToList();

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

Source: https://habr.com/ru/post/1722441/


All Articles