0

i new to MVC and LINQ and i am learning how to use AngularJs and MVC for a new project that i have been assigned. So i am learning these technologies through an online video.The tutor uses code first approach, so Visual Studio Creates all the necessary files and a database that matches the tutors models, i on the other hand already have a database and i created the Api's to perform REST operations against the DB. So when i run this code below

        public IEnumerable<Todo> GetTodos()
    {

        return Models.DbStoredProcModel.GetAllTodos.GetAllTodoEntryStoredProcedure();
    }

it return a Json output like this

enter image description here

So up to this point, my json output matches with the video tutorial even though our code approach is different. So the tutor, using this piece of code he wrote

Tutors Code:

    public IEnumerable<Todo> GetTodos(string q= null,string sort = null, bool desc = false, int ? limit = null, int offset =0)
    {
        var result = ((IObjectContextAdapter)db).ObjectContext.CreateObjectSet<Todo>();

        IQueryable<Todo> items = string.IsNullOrEmpty(sort)
            ? result.OrderBy(o => o.Priority)
            : result.OrderBy(String.Format("it. {0} {1}", sort, desc ? "DESC" : "ASC"));

        if (!string.IsNullOrEmpty(q) && q != "undefined")
            items = items.Where(t => t.Text.Contains(q));

        if (offset > 0) items = items.Skip(offset);
        if (limit.HasValue) items = items.Take(limit.Value);

        return items;
    } 

it will allow him to perform Search and Sort operations on the data returned.

now here is my own code trying to code trying to do the same.

       public IEnumerable<Todo> GetTodos(string q= null,string sort = null, bool desc = false, int ? limit = null, int offset =0)
    {
        var result = Models.DbStoredProcModel.GetAllTodos.GetAllTodoEntryStoredProcedure( );

        IQueryable<Todo> items = string.IsNullOrEmpty(sort)
            ? result.OrderBy(o => o.Priority)
             // i get the error on the line below
            : result.OrderBy(String.Format("it. {0} {1}", sort, desc ? "DESC" : "ASC"));

        if (!string.IsNullOrEmpty(q) && q != "undefined")
            items = items.Where(t => t.Text.Contains(q));

        if (offset > 0) items = items.Skip(offset);
        if (limit.HasValue) items = items.Take(limit.Value);

        return items;

    }

however, i get an error saying that

Error:The type arguments for method 'System.Linq.Enumerable.OrderBy<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

How do i fix this?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
nahaelem
  • 133
  • 2
  • 4
  • 13
  • I'm guessing that because you're using a stored procedure (which doesn't return IQueryable), trying to assign it to `items` fails. Really, you should be passing the parameters to the stored procedure. – markpsmith Jan 09 '15 at 15:03
  • 1
    Are you sure that there is a version of the OrderBy extesion which accepts a string? http://msdn.microsoft.com/pt-br/library/system.linq.queryable.orderby(v=vs.110).aspx – gustavodidomenico Jan 09 '15 at 15:06
  • Same here, I'm not aware of an "OrderBy" method that accepts a string as a parameter. You should provide more information. – Matteo Mosca Jan 09 '15 at 15:10
  • like i said, i am following his code. and yes 'markpsmith' i am using a storedprocedure. i will pass try passing the data to the stored proc instead. – nahaelem Jan 09 '15 at 15:12
  • @user3761259 - it would be better to directly ask whoever created tutorial you are using. It is possible to make code you've posted to compile (i.e. by writing custom extension `OrderBy(this IEnumerable s, string s)` or by removing the line altogether), but it is very unclear what result is expected. – Alexei Levenkov Jan 09 '15 at 15:33
  • 1
    Specify the return type from GetAllTodoEntryStoredProcedure instead of *var* result. It possibly isn't IEnumerable. – Kaido Jan 09 '15 at 15:44
  • @Kaido It's an `IQueryable`, or something that implements it. We know that given that the LINQ `OrderBy` is working correctly. I don't know what else you expect to get out of knowing the specific type. – Servy Jan 09 '15 at 16:02
  • 1
    possible duplicate of [How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?](http://stackoverflow.com/questions/307512/how-do-i-apply-orderby-on-an-iqueryable-using-a-string-column-name-within-a-gene) – Hamlet Hakobyan Jan 09 '15 at 16:07

1 Answers1

-1

Change

 result.OrderBy(String.Format("it. {0} {1}", sort, desc ? "DESC" : "ASC"))

to

 result.OrderBy<Todo,string>(String.Format("it. {0} {1}", sort, desc ? "DESC" : "ASC"))

for a more clear error message.

The underlying issue is perhaps that

result.OrderBy(String.Format("it. {0} {1}", sort, desc ? "DESC" : "ASC"))

doesn't have a meaningful interpretation in the context of your application. It looks like dynamic linq to me, requiring references that pull in alternative OrderBy definitions than the ones you will have referenced.

My suggestion would be to check the namespaces referenced by the tutorial (in the project references and at the top of this file), in case you have missed any.

Kaido
  • 3,383
  • 24
  • 34
  • 2
    This isn't going to change anything. That line of code was already correctly inferring the generic arguments. – Servy Jan 09 '15 at 15:52
  • Oh yes I see now, I'll update to the line in question – Kaido Jan 09 '15 at 16:15
  • 1
    This is still nowhere near an answer to the question. Clearly the intent is to not use the `Enumerable.OrderBy` method, so the real implementation isn't even going to have two generic arguments. – Servy Jan 09 '15 at 16:17