Add column in IEnumerable in C #

I don't think I can add a field (column) to an existing IEnumerable. But I want a new IEnumerable that is derived from an existing IEnumerable with a computed field. The pseudocode in WebMatrix using web pages looks like this:

var db = Database.Open("LOS"); var ie = db.Query(sqlAssignments); // make a new ie2 that has an extra field // ??? ie2 <=== ie with new field c = ie.a + ie.b var grid = new WebGrid( ie2, extra parameters ); 

I know how to do this by looping through all the lines in ie. But I hope that there is something more elegant.

+6
c # linq webmatrix
source share
5 answers

What about:

 var ie2 = ie.Select(x => new { x.Foo, x.Bar, Sum = x.Abc + x.Def }); var grid = new WebGrid(ie2); 
+6
source share

You can do this using Select . Try the following:

 ie2 = ie.Select( e => new { IE = e, NewParam = e.X+eY }); 
+4
source share
 ie2 = ie.Select(v => new {va, vb, c = va + vb}); 
+3
source share

Using Linq!

 var newIe = from item in ie select new {item.NewField, item.OldFiedl1 etc... } 

Also, it might be best (if you intend to use outside of this method) to assign to an anonymous type.

+2
source share

First of all, IEnumerable is probably a list of something - an object. This is an object that you can expand.

Perhaps you can do something like this:

 var ie = db.Query( ... ); var ie2 = ie.Select(i => new MyIe2Object { Prop1 = i.Prop1, NewProp = i.Prop1 + i.Prop2 }); 
+2
source share

All Articles