Add a column in IEnumerable in C # to make it work with WebGrid

In this question, Add a column in 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" ); 

Data is displayed correctly, but the grid is no longer sorted. It is sorted fine if you pass, i.e. Instead of ie2. I have a problem if I do ToList () or not. Obviously, there is some difference between ie and ie2. Here's GetType for ie:

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

and for 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]] 

What should I do with ie2 so that it works with WebGrid and is sorted correctly?

+1
c # linq webmatrix
source share
1 answer

Try creating a type for ie2 instead of using an anonymous type, for example:

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

Where is the new type:

 class MyNewType { public string ACCT { get; set } public int RefID { get; set } public int Color { get; set } } 
+2
source share

All Articles