4

I currently have:

var ids = dt.AsEnumerable().Select(x => (int)x["Id"]).ToList();

However I also need to retreive another column, name: "level" of type int also. expecting output something like:

var<int,int> ids = ....
user2906420
  • 1,249
  • 6
  • 27
  • 44
  • or, since it's just `Linq-To-Objects`: http://stackoverflow.com/questions/1202981/select-multiple-fields-from-list-in-linq – Tim Schmelter Feb 04 '14 at 13:40

1 Answers1

7

One approach would be an anonymous type:

var ids = dt.AsEnumerable().Select(x => new
{
    Id = (int)x["Id"],
    Level = (int)x["level"]
}).ToList();

This will give you a List<> of that anonymous type, so now you could do something like this:

var level = ids[0].Level

UPDATE: if you're going to have to store them in Session for persistence then I'd recommend building a real type (class), let's call it Foo for this example. That would change the code to:

var ids = dt.AsEnumerable().Select(x => new Foo
{
    Id = (int)x["Id"],
    Level = (int)x["level"]
}).ToList();

Then when you need to get them out of Session:

var ids = (List<Foo>)Session["ids"];
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232