how to loop through a session object which is a list

I have a session object, which contains a list of objects..

Session.Add("errorlist",errorlist);

Now i want to loop through this errorlist in another function. I tried, but it gives following error:

foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

This is what I tried:

var error = Session["errorlist"];
foreach (var item in error)
{
    //Something here
}

I can see a list of objects in "error" variable.

Answers


Everything that goes into session is of type System.Object by default. So your var statement will not have the correct type.

You need to cast it when pulling back out of session.

var error = (List<MyObject>)Session["errorlist"];

A better way would be to use a safe cast and check for null:

var error = Session["errorlist"] as List<MyObject>;

if(error != null){
   //Do stuff here
}

Need Your Help

how to find the interrupt source code in linux kernel?

c linux linux-kernel timestamp interrupt

I am looking for source code of interrupt service routine and searching net_bhi(); and netif_rx(); interrupt routine in the linux kernel. The above both api are the packet receiving of udp in the l...

MouseMove event too slow for painting

c# .net winforms xna

I'm using C# WinForms to create a level builder for my XNA game. I have a tile grid that you can paint with a Pencil tool, like in MSPaint. The problem is that when you drag the mouse fast(ish) to ...