markzzz
markzzz

Reputation: 48035

Can't use "var" variables in the .ascx?

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?

Upvotes: 0

Views: 618

Answers (1)

Adrian Thompson Phillips
Adrian Thompson Phillips

Reputation: 7158

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:

public CustomerContactViewModel()
{
    public string Name { get; set; }

    public string Phone { get; set; }
}

and then update your LINQ query to:

IEnumerable<CustomerContactViewModel> custQuery =
    from cust in customers
    where cust.City == "Phoenix"
    select new CustomerContactViewModel() { Name = cust.Name, Phone = cust.Phone };

Upvotes: 3

Related Questions