1

I'm declaring my variable with this code

IEnumerable<object> Update_Select_Result= null;

and assigning with this

Update_Select_Result = DB.Query(CMD_Select, Request["UpdateID"]);

but how can I use this variable in foreach?

4
  • Is there a particular reason you need to iterate on IEnumerable<object> instead of List<specificType>? In addition, where is the Razor part of the question? Commented May 29, 2013 at 17:52
  • foreach (object x in Updrate_Select_Result) { ... } but don't use object Commented May 29, 2013 at 17:59
  • @DavidL I want assign null to "Update_Select_Result" because I'm using this variable to all part of the program so this variable must declare at the top of the codes and sometime assign assign with select sql query . I'm using this codes but , If you have better solution help me please . Commented May 29, 2013 at 18:03
  • @Exceptyon Thank you . I find what is the problem with your help . THANKS ;) Commented May 29, 2013 at 18:08

1 Answer 1

1

First, a type OTHER than object should be used if at all possible. Build a specific class that mirrors your return output and wrap it in a List.

public class ReturnType
{
    public int UpdateId { get; set; }
    public string WhateverString { get; set; }  
}


List<ReturnType> Update_Select_Result = null;
Update_Select_Result = DB.Query(CMD_Select, Request["UpdateID"]);

Note, you will probably have to modify your data access. another option is to construct a new object from your return from the database.

At the top of your razor view, assign Model your new type

@model List<ReturnType>

Now that you have your model assigned, you can access it directly with "Model". Within your razor syntax, iterate over the collection and perform whatever magic you need to.

@foreach (var x in Model)
{
    // do whatever you need with each member of Model (List<ReturnType>), 
    // represented by x

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.