LINQ - does .Cast <T> () Select records?
I noticed that a specific command forces LINQtoSQL to connect to the database and load records that are part of the query, for example .ToArray ().
Does the .Cast () command execute a request (and how can I say this in the future?). For example...
IRevision<T> current = context.GetTable(typeof(T))
.Cast<IRevision<T>>()
.SingleOrDefault(o => o.ID == recordId);
I know that there is a command for .GetTable that allows you to specify a generic type, but for strange and inexplicable reasons, it cannot be used in this situation.
From Note Enumerable.Cast () :
. - , , . , , , , GetEnumerator , foreach Visual # Visual Basic.
LINQ , . , LINQ, :
- Count
- ElementAt
- ElementAtOrDefault
- FirstOrDefault
- Last
- LastOrDefault
- LongCount
- Max
- Min
- SequenceEqual
- Single
- SingleOrDefault
- ToArray
- ToDictionary
- ToList
- ToLookup
What you are looking for is called Delayed Execution. Statements that delay execution are executed only when they try to access data. Expressions such as ToList are executed immediately because the data is needed to convert it to a list.
Cast can wait until you actually get access to it, so this is a deferred expression.