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
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?