I don't think I can actually add a field (column) to an existing IEnumerable. But what I want is a new IEnumerable that is derived from an existing IEnumerable with a calculated field. The pseudocode in WebMatrix using Web P开发者_如何学编程ages looks like:
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 looping through all the rows in ie. But I'm hoping there's something more elegant.
How about:
var ie2 = ie.Select(x => new { x.Foo, x.Bar, Sum = x.Abc + x.Def });
var grid = new WebGrid(ie2);
You can do this using Select
. Try this:
ie2 = ie.Select( e => new { IE = e, NewParam = e.X+e.Y });
ie2 = ie.Select(v => new {v.a, v.b, c = v.a + v.b});
Using Linq!
var newIe = from item in ie
select new {item.NewField, item.OldFiedl1 etc... }
Also, probably best (if you intend to use outside this method) to make that anonymous type named.
First of all, the IEnumerable
is probably a list of something - an object. That is the object you can extend.
You can probably do something like this:
var ie = db.Query( ... );
var ie2 = ie.Select(i => new MyIe2Object {
Prop1 = i.Prop1,
NewProp = i.Prop1 + i.Prop2
});
精彩评论