Passing data in ASP.NET MVC using LINQ - nuttiness?

Var is a compiler shortcut for declaring a type in the current scope. It doesn't add any dynamic functionality to the . NET runtime, so code outside the scope sees the object as "System.

Object", since that is the most-specific type in the inheritance chain the view code is aware of You should create a real class if you want to pass tuple objects around; that's what you had to do before var so it's not like you're losing anything by having to do it now :).

Var is a compiler shortcut for declaring a type in the current scope. It doesn't add any dynamic functionality to the . NET runtime, so code outside the scope sees the object as "System.

Object", since that is the most-specific type in the inheritance chain the view code is aware of. You should create a real class if you want to pass tuple objects around; that's what you had to do before var, so it's not like you're losing anything by having to do it now :).

Great answer...thanks! However, it does seem silly to me that you would have to refactor a class should you change your query. Any suggestions on doing query testing?

– Brandon Watson Feb 9 '09 at 18:04.

The best answer here simply is to create a new type. You can do get anonymous types back from object (see here) but it is ugly and brittle. Don't do it!

You could use reflection (kvp.GetType(). GetProperty("strWord"). GetValue(kvp, null)) - but that also isn't a great idea.In this case - perhaps use the existing KeyValuePair from the original select?

Or your own Tuple?

Don't be afraid of new types. They give lots of other benefits too especially when testing. Use the new property syntax and your class will only be as many lines long as you have properties.

You can lose that awful Hungarian notation too. Int and str - yuk.

– Brandon Watson Feb 9 '09 at 18:07 @brandon mid 80s for me :-) however I do dislike the way MS convention is to go completely without prefixes. I like to use them for UI controls, such as txtName or cbAcceptTerms for a checkbox. I had your exact same concern myself so I know the feeling - I wonder how 'dynamic' will change the rules – Simon_Weaver Feb 9 '09 at 19:21.

Allow me to start with: I am a n00b on ASP.NET MVC. I love it, but I am a n00b. I am trying to pass "complex" data back from a LINQ query.

I understand how to use the data context and then just cast that data when I send it back, but when I do a more complicated LINQ query which returns an anonymous type, things break down. I saw someone ask a similar question (http://stackoverflow.com/questions/278941/mvc-linq-to-sql-table-join-record-display), and the answer seemed to be to create a new data type to capture the data from the LINQ query. I don't get that I can create a var type in the controller, and access the member fields within the controller, but if I want to pass that var back to my View, I need to create an entire new class for that.

So the data is getting to the View, but I cannot refer to the data by the field names I assigned in the LINQ query. But I am getting a compile error on the kvp.strWord. I can reference the fields of my variable kvp without a compile error.

So something is getting lost when passing from the Controller to the View. Am I forgetting to include something somewhere? Perhaps a “using” or in the “ where “datatype” = your data context type, and you are all good.

When you reduce your data set into something new in LINQ, I can't believe that the best answer is to create a new class so that I can use it in my View.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions