Traversing a nested LINQ query in of anonymous objects in C# view via ASP.NET MVC?

I think you might be overcomplicating the solution, based on what I think you're trying to do. Ultimately it would be cool to write a little class to handle this type of data structure, where you want to aggregate the dates based on year and month I'm assuming that you want to leave out years / months that are empty, or that your date range is unlikely to have gaps. You'll also notice that I'm using IQueryable, rather than IEnumerable Basically you need to distinct the years in your range, and then distinct the months in each of the years.So, given this data set: List dateRange = new List(); dateRange.

Add(new DateTime(2009, 5, 18)); dateRange. Add(new DateTime(2009, 5, 12)); dateRange. Add(new DateTime(2009, 6, 22)); dateRange.

Add(new DateTime(2009, 2, 14)); dateRange. Add(new DateTime(2009, 11, 23)); dateRange. Add(new DateTime(2010, 5, 2)); dateRange.

Add(new DateTime(2010, 7, 17)); dateRange. Add(new DateTime(2010, 7, 10)); dateRange. Add(new DateTime(2009, 11, 28)); dateRange.

Add(new DateTime(2009, 11, 16)); dateRange. Add(new DateTime(2010, 1, 30)); dateRange. Add(new DateTime(2010, 3, 1)); dateRange.

Add(new DateTime(2010, 9, 23)); dateRange. Add(new DateTime(2010, 5, 1)); dateRange. Add(new DateTime(2010, 9, 4)); ViewData"range" = dateRange.AsQueryable() You could iterate through it in your view by doing: % var range = ViewData"range" as IQueryable; %> x.

Year) .Distinct() . OrderBy(x=>x)) { %> x. Year==year) .

OrderBy(x=>x. Month) . Select(x=>x.

Month) .Distinct()) { %> x. Year == year && x. Month == month)) { %.

I think you might be overcomplicating the solution, based on what I think you're trying to do. Ultimately it would be cool to write a little class to handle this type of data structure, where you want to aggregate the dates based on year and month. I'm assuming that you want to leave out years / months that are empty, or that your date range is unlikely to have gaps.

You'll also notice that I'm using IQueryable, rather than IEnumerable. Basically you need to distinct the years in your range, and then distinct the months in each of the years. So, given this data set: List dateRange = new List(); dateRange.

Add(new DateTime(2009, 5, 18)); dateRange. Add(new DateTime(2009, 5, 12)); dateRange. Add(new DateTime(2009, 6, 22)); dateRange.

Add(new DateTime(2009, 2, 14)); dateRange. Add(new DateTime(2009, 11, 23)); dateRange. Add(new DateTime(2010, 5, 2)); dateRange.

Add(new DateTime(2010, 7, 17)); dateRange. Add(new DateTime(2010, 7, 10)); dateRange. Add(new DateTime(2009, 11, 28)); dateRange.

Add(new DateTime(2009, 11, 16)); dateRange. Add(new DateTime(2010, 1, 30)); dateRange. Add(new DateTime(2010, 3, 1)); dateRange.

Add(new DateTime(2010, 9, 23)); dateRange. Add(new DateTime(2010, 5, 1)); dateRange. Add(new DateTime(2010, 9, 4)); ViewData"range" = dateRange.AsQueryable(); You could iterate through it in your view by doing: x.

Year) .Distinct() . OrderBy(x=>x)) { %> x. Year==year) .

OrderBy(x=>x. Month) . Select(x=>x.

Month) .Distinct()) { %> x. Year == year && x. Month == month)) { %> Ideally you just want to either use extension methods or a wrapper class so you could do: foreach(var year in range.DistinctYears()) The months something that wraps the English format around the integer value would be nice too.

See how that works out for you.

Brixton, thank you so much! With your help, it worked out exactly how I need it. I feel like all the bits that I know about ASP.NET are starting to connect together for me :) Cheers!

– Neofizz Jan 29 '10 at 16:22.

Range. OrderBy(r => r. Year).

OrderBy(r => r. Month)...

I would like for this to be considered as "answered" I have been able to use to progress the development of my application. It's really helped me to see how LINQ results can be used in the default view rendering engine. A topic which I feel is a little under documented.

Thanks!

Yes, but your view won't be strongly typed. But the helpers will work. That textbox should render " it worked!" as the value.

So can you define a view where T is defined by what gets passed to it from the controller? Well yes, but not at compile time obviously. Think about it for a moment.

When you declare a model type for a view, it's so you get intellisense for the view. That means the type must be determined at compile time. But the question asks, can we determine the type from something given to it at runtime.

Sure, but not with strong typing preserved. How would you get Intellisense for a type you don't even know yet? The controller could end up passing any type to the view while at runtime.

We can't even analyze the code and guess, because action filters could change the object passed to the view for all we know. I hope that clarifies the answer without obfuscating it more.

I think you might be overcomplicating the solution, based on what I think you're trying to do. Ultimately it would be cool to write a little class to handle this type of data structure, where you want to aggregate the dates based on year and month.

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