Add a column to an IEnumerable in c#
Asked Answered
M

5

8

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 Pages 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.

Momus answered 17/3, 2011 at 11:41 Comment(2)
IEnumerable simply signifies an enumerable type, i.e. a consumer can enumerate through it's constituent items. It has no concept of 'fields' or 'columns'.Aplanospore
@Adam - yet I think more people are likely to understand that title than, for example: "Use a projection to create a new sequence based on an old sequence, but with additional members"...Epidaurus
E
6

How about:

var ie2 = ie.Select(x => new { x.Foo, x.Bar, Sum = x.Abc + x.Def });
var grid = new WebGrid(ie2);
Epidaurus answered 17/3, 2011 at 11:44 Comment(4)
this works great, but bizarrely has led to a new problem where the WebGrid no longer automatically sorts :( . I decided to make it a new question.Momus
@Momus - just use ie2.ToList()Epidaurus
I tried ToList, but it still didn't sort. Rather than morph this question into the new question, i asked a new question. #5339835Momus
that's kind of a pain: what if you have like 20 columns or more?Iphigenia
O
4

You can do this using Select. Try this:

ie2 = ie.Select( e => new { IE = e, NewParam =  e.X+e.Y });
Obelia answered 17/3, 2011 at 11:45 Comment(3)
The one comment I have about this is that most data-binding only looks one level down; you usually need to flatten the properties of e, otherwise you'll just have a column named e with e.ToString() as the values.Epidaurus
If that is the case, you will have to flatten as your answer depicts. If it only looks down one level, let's hope the number of properties on e is manageable :)Wenn
I didn't get this to immediately work, and decided to flatten it. Fortunately, there are only about 10 properties on e.Momus
G
3
ie2 = ie.Select(v => new {v.a, v.b, c = v.a + v.b});
Glower answered 17/3, 2011 at 11:44 Comment(0)
G
2

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.

Geithner answered 17/3, 2011 at 11:45 Comment(0)
L
2

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
});
Latona answered 17/3, 2011 at 11:46 Comment(2)
When LINQ gets involved, I don't think it is true to guess a "list" - but sure, extending the something would work, tooEpidaurus
The point is that it's not really about extending the IEnumerable, but the something.Carrasco

© 2022 - 2024 — McMap. All rights reserved.