Use IQueryable on stored procedure in Entity Framework

2.7k Views Asked by At

I'm trying to use IQueryable on the stored procedure, the reason for writing a stored procedure is to check multiple conditions in the query. Please correct me if I'm going in a wrong direction.

I assume applying IQueryable will be much faster than the IEnumerable not to forget I was able to use IQueryable on the other entities without any issues.

Controller:

public ActionResult CrmBlogGroupType(int? page, bool? Name, bool? AuthorTitle, bool? Description, string search, int? PageSize, string type)
{
    try
    {
        if (type==null)
        {
            type = "A";
        }

        IQueryable<Usp_getBlogSetPosts_Result> _objBlogSet = _dataLayer.GetBlogSet(type);
        var Result = _objBlogSet.ToPagedList(page ?? 1, PageSize ?? 10);

        return View(_objBlogSet);
}

Data layer:

public IQueryable<Usp_getBlogSetPosts_Result> GetBlogSet(string type)
{
        IQueryable<Usp_getBlogSetPosts_Result> Obj = _dbContext.Usp_getBlogSetPosts(type).AsQueryable();
         return Obj;
}

Context.cs:

 public virtual ObjectResult<Usp_getBlogSetPosts_Result> Usp_getBlogSetPosts(string blogSetType)
 {
        var blogSetTypeParameter = blogSetType != null ?
            new ObjectParameter("BlogSetType", blogSetType) :
            new ObjectParameter("BlogSetType", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Usp_getBlogSetPosts_Result>("Usp_getBlogSetPosts", blogSetTypeParameter);
}

Error:

The result of a query cannot be enumerated more than once

1

There are 1 best solutions below

0
On

this is because a stored procedure output result set is not of the type IQueryable.. you have to use IEnumerable in case of stored procedures to work