Does Cast <T> call IQueryable to evaluate in Linq-to-SQL? What are the restrictions on its use?

Can Cast force IQueryable to evaluate in Linq-to-SQL?

What are the restrictions on its use?

It seems that if I created an explicit conversion to write to a domain object, for example:

  IQueryable<TSqlRecord> q;
  var cast = q.Cast<TDomainObject>();

Then I should get an error because Linq-to-Sql and obviously cannot handle the constructor of the domain object.

But can I apply IQueryable<TSqlRecord>to its interface IHasIntIdwithout forcing the request to evaluate?

+3
source share
2 answers

The listing is delayed and does not cause immediate execution. This is due to the fact that he does not list the source himself. Only when something is listed is the query executed.

. .

- , . , . , , , - new'd.

class SpecificObject : DomainObject

MyDataContext myDC = new MyDataContext();
IQueryable<DomainObject> query = myDC.SpecificObjects.Cast<DomainObject>();

Console.WriteLine(myDC.GetCommand(query).CommandText);

- - , .


Downcasting . LinqToSql , T Table<T>. - TChild. , .

class SpecificObject : DomainObject

MyDataContext myDC = new MyDataContext();
IQueryable<SpecificObject> query = myDC.DomainObjects.Cast<SpecificObject>();

Console.WriteLine(myDC.GetCommand(query).CommandText);

... . LinqToObject Enumerable.Cast<T> (. ). , LinqToSql, -.net casting - , # sql. , .

+4

, IQueryable<TSqlRecord> IHasIntId, IQueryable<TSqlRecord> TSqlRecord, IHasIntId . , , .
, :

IQueryable<TSqlRecord> IQueryable<IHasIntId>.

: ,
MSDN :

Cast (IQueryable) MethodCallExpression, Cast (IQueryable) . MethodCallExpression CreateQuery (Expression) IQueryProvider, Provider .

, , Cast (IQueryable), . , TResult.

, Cast , , , SQL.

+2
source

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


All Articles