将列添加到c#中的IEnumerable中,以便它可与WebGrid一起使用 [英] Add a column to an IEnumerable in c# such that it works with WebGrid

查看:78
本文介绍了将列添加到c#中的IEnumerable中,以便它可与WebGrid一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题中,在C#中为IEnumerable添加一列,我得到以下代码:

In this question, Add a column to IEnumerable in C#, I got the following code:

var db = Database.Open("LOS"); 
var ie = db.Query(sqlAssignments); 

// make a new ie2 that has an extra calculated field
var ie2 = processes.Select( e => new { e.ACCT,  e.RefID, color = e.RefID + 9000000 });

var grid = new WebGrid(ie2.ToList(), rowsPerPage : 50, ajaxUpdateContainerId : "grid" );

数据正确显示,但是网格不再排序.如果您通过ie,而不是ie2,则排序很好.如果我不执行ToList(),就会出现问题.显然,ie和ie2之间有一些区别.这是ie的GetType:

The data correctly shows up, however the grid no longer sorts. It sorts fine if you pass ie, instead of ie2. It has the problem if I do the ToList() or not. Obviously there's some difference between ie and ie2. Here's the GetType for ie:

System.Collections.ObjectModel.ReadOnlyCollection`1[System.Object]

对于ie2:

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Object,<>f__AnonymousType0`10[System.Object,System.Object,System.Object,System.Object,System.Object,System.Object,System.Object,System.Object,System.Object,System.Object]]

我应该如何对ie2进行处理才能使其与WebGrid一起使用并正确排序?

What should I do to ie2 to make it work with WebGrid and sort correctly?

推荐答案

尝试为ie2创建类型,而不要使用匿名类型,例如:

Try to create a type for ie2 instead of using an anonymous type, like:

var ie2 = processes.Select( e => 
   new MyNewType { ACCT = e.ACCT, RefID = e.RefID, Color = e.RefID + 9000000 }
);

新类型为:

class MyNewType {
   public string ACCT { get; set }
   public int RefID { get; set }
   public int Color { get; set }
}

这篇关于将列添加到c#中的IEnumerable中,以便它可与WebGrid一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆