Add a column to an IEnumerable in c#

穿精又带淫゛_ 提交于 2019-12-05 07:58:48

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
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!