On .ascx.cs I have this code, for example :
var xDoc = XDocument.Parse(xml); //or XDocument.Load(fileName)
var list = xDoc.Descendants("ordinanza")
.Select(n => new
{
Numero = n.Element("numero").Value,
Titolo = n.Element("titolo").Value,
})
.ToList();
Well, now I’d like to “foreach” this anonymous type on my .ascx, but I can’t use protected/public for list (because is var).
So, how can I do it?
The data you’re extracting is a cut-down version of a larger entity, and you’re using this data in a view. This in MVC or MVP terms would be a view model (a type of data transfer object for displaying data in the UI).
What you could do is create a simple lightweight class (view model) to hold this data:
and then update your LINQ query to: