开发者

Sorting a List<T> using LINQ

开发者 https://www.devze.com 2023-02-24 04:06 出处:网络
List<DatsWussup.Models.Message> messages = mc.GetMessages(); List<DatsWussup.Models.JQGridMessage> gridMessages = FormatMessages(messages);
List<DatsWussup.Models.Message> messages = mc.GetMessages();
List<DatsWussup.Models.JQGridMessage> gridMessages = FormatMessages(messages);

int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = gridMessages.Count;
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

var questions = gridMessages
    .OrderBy(sidx + " " + sord)
    .Skip(pageIndex * pageSize)
    .Take(pageSize);

So I am following along with the JqGrid and MVC guide here : http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx and in the step where he adds sorting/paging, I took the above code from the blog.

Now, you can probably see what I am trying to do just from looking at the code, especially if you are familiar with working with JqGrids and MVC together. However, I'm getting this error:

The type arguments for method 'System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable, System.Func)' cannot be 开发者_运维技巧 inferred from the usage. Try specifying the type arguments explicitly.

When I try to compile the above code. I'm not very good with LINQ or any delegates in general, could I get a little help?

Thanks!


OrderBy() takes either a Func<TSource, TKey> delegate, an expression Expression<Func<TSource, TKey>> or if you have DLINQ, a string. The blog mentions that you need DLINQ and links to the download page and ScottGu's article. Apparently you don't have it.

Download it, add LinqSamples\DynamicQuery\DynamicQuery\Dynamic.cs to your project and use the System.Linq.Dynamic namespace and it should be made available to you.


OrderBy is expecting a lambda expression, maybe something like this:

var questions = gridMessages
    .OrderBy(m => m.sidx + " " + m.sord)
    .Skip(pageIndex * pageSize)
    .Take(pageSize);
0

精彩评论

暂无评论...
验证码 换一张
取 消