1

is there a way in C# to store several different view model objects in a var array?

example:

var user_model = db.Database.SqlQuery<User>("SELECT * FROM [User]").ToList();
var product_model = db.Database.SqlQuery<Product>("SELECT * FROM [Product]").ToList();

var my_models = new[]{ user_model, product_model };
string[] object_titles = { "User" , "Product" };

for( int i=0 ; i<my_models.Length ; i++ ){

if( my_models[i].Count() != 0 )

    return View("../"+ object_titles[i] +"/Index", my_models[i]);

}

Unfortunately the example above does not work and I do not know how to solve that problem. Could anyone give me a hint, how to solve it?

5
  • Is there any exception you got? Commented Jun 24, 2013 at 4:52
  • "does not work" is an absolutely horrible description of whatever problem you are having, and I'm pretty sure it's not the text of any error message you're getting either... Commented Jun 24, 2013 at 4:55
  • What is this code supposed to do ? I'm a bit confused ? Commented Jun 24, 2013 at 4:59
  • 1
    @DimitarDimitrov Yeah I was thrown for a loop too (no pun intended). On my third read through I finally understood they want to return the view for the first model that has data in it. I think... Commented Jun 24, 2013 at 5:01
  • Sorry, that i did not describe the purpose of my code. The example above is just a very simple version of the original code. Im programming a search function which uses the sql queries (WHERE LIKE('%my_name%')...) to go through the database tables to find any results. If it finds a username or something else it returns the Index View (list) of that object, where you can edit or delete the found result that is now displayed in the index view list. Commented Jun 24, 2013 at 5:28

2 Answers 2

3

Forgive me if I missunderstood, but, wouldn't this be much, much easier:

var user_model = db.Database.SqlQuery<User>("SELECT * FROM [User]").ToList();
var product_model = db.Database.SqlQuery<Product>("SELECT * FROM [Product]").ToList();

if(user_model.Any())
{
    return View("../User/Index", user_model);
}
else if(product_model.Any())
{
    return View("../Product/Index", product_model);
}
// return a default view or some other magic ...
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, i actually solved it like that but Im using much more objects than two. so it would be a very long if statement. ;)
1
var my_models = new IEnumerable<object>[] { user_model, product_model };

try this

1 Comment

You are better off using interfaces or base classes. Casting back to an object every time isn't the best approach since looking at your list on it's own really gives no indication of what type of objects you are looking at / for and makes it more difficult to program against in other areas.

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.