在c#中向IEnumerable添加一列

Kno*_*nox 7 c# linq webmatrix

我认为我实际上不能将字段(列)添加到现有的IEnumerable中.但我想要的是一个新的IEnumerable,它是从一个带有计算字段的现有IEnumerable派生出来的.使用网页的WebMatrix中的伪代码如下所示:

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 );
Run Code Online (Sandbox Code Playgroud)

我知道如何循环遍历ie中的所有行.但我希望有更优雅的东西.

Mar*_*ell 6

怎么样:

var ie2 = ie.Select(x => new { x.Foo, x.Bar, Sum = x.Abc + x.Def });
var grid = new WebGrid(ie2);
Run Code Online (Sandbox Code Playgroud)

  • 那是一种痛苦:如果你有20列或更多列怎么办? (2认同)