views:

795

answers:

2

I want to get IQueryable<> result when executing stored procedure.

Here is peace of code that works fine:

IQueryable<SomeEntitiy> someEntities;  
var globbalyFilteredSomeEntities = 
  from se in m_Entities.SomeEntitiy
    where
      se.GlobalFilter == 1234 
  select se;

I can use this to apply global filter, and later use result in such way

result = globbalyFilteredSomeEntities
  .OrderByDescending(se => se.CreationDate)
  .Skip(500)
  .Take(10);

What I want to do - use some stored procedures in global filter.
I tried:

Add stored procedure to m_Entities, but it returns IEnumerable<> and executes sp immediately:

var globbalyFilteredSomeEntities = 
  from se in m_Entities.SomeEntitiyStoredProcedure(1234);

Materialize query using EFExtensions library, but it is IEnumerable<>.
If I use AsQueryable() and OrderBy(), Skip(), Take()
and after that ToList() to execute that query -
I get exception that DataReader is open and I need to close it first(can't paste error - it is in russian).

var globbalyFilteredSomeEntities = 
  m_Entities.CreateStoreCommand("exec SomeEntitiyStoredProcedure(1234)")
            .Materialize<SomeEntitiy>();
            //.AsQueryable()
            //.OrderByDescending(se => se.CreationDate)
            //.Skip(500)
            //.Take(10)
            //.ToList();   

Also just skipping .AsQueryable() is not helpful - same exception.
When I put ToList() query executes,
but it is too expensive to execute query without Skip(), Take().

A: 

sually you can get around these issues with ToList()

var globbalyFilteredSomeEntities = m_Entities.CreateStoreCommand("exec SomeEntitiyStoredProcedure(1234)") 
                                             .Materialize<SomeEntitiy>()
                                             .ToList()  // <<-- added this.
                                             .WhateverYouWant();

Why can't you do the Skip() and Take() on the enumerable? Doing this will only download the results that are skipped or taken, the others won't be read.

Edit: The previous version was plain wrong in many aspects.

erikkallen
It is not what I'm looking for. When I put `ToList()` query executes, but it is too expensive to execute query without `Skip()`, `Take()`.
mmcteam.com.ua
The AsEnumerable() solution should do the trick in this case (after edit)
erikkallen
(To erikkallen) But it is already IEnumerable.
mmcteam.com.ua
+2  A: 

You can't do what you're trying to do, for the same reason that you can't put a stored procedure in a FROM clause of a SELECT query - SQL isn't built to support this kind of operation.

Could you put the logic you want into a view instead of a stored procedure?

Damien_The_Unbeliever
Nice idea. Will EF return same type of entity from the view, or I have to do some more steps to achieve that? Cane you share some code snippet working with view in ef?
mmcteam.com.ua
Without EF, there is no problem doing what he wants to, though.
erikkallen
Would be nice to see usage example of Database View in EF. I'm not interested doing it without EF.
mmcteam.com.ua