Server-side execution of Entity Framework Query co

2019-06-13 16:15发布

问题:

Is it possible to call a StoredProcedure from an ObjectQuery? Basically I want to dynamically build a query and execute it server-side. You can imagine each query to be part of a search where you can combine different criteria with "and" or "or". It is working fine with ObjectQueries created like this.

var query1 = from a in objectContext.Articles
where a.Name = 'SOMETHING'
select new ResultType { ArticleId = a.ArticleId, Name = a.Name };

var query2 = from a in objectContext.Articles
where a.Name = 'SOMETHING ELSE'
select new ResultType { ArticleId = a.ArticleId, Name = a.Name };

query1 = query1.Intersect(query2); // or union depending on what we need

// ... and more queries

var something = query1.ToList(); // query execution...

So how I can get this working with a stored procedure call. The problem is that the call to ExecuteFunction will return an ObjectResult.

回答1:

I don't see any stored procedure call in your code but if you want to combine ObjectQuery or Linq-to-entities query with stored procedure the answer is simple: not possible. The reason is same as in SQL. You cannot work with result set of stored procedure call in SQL.

You also cannot define ObjectQuery / Linq-to-entities query as stored procedure.