I'm creating an object with LINQ by parsing values from an XDocument. It is my understanding that objects should be created to be immutable unless you really need to change the values later on, so I've made private setters.
public class GeoLookupResult
{
public string LocationType { get; private set; }
public string Country { get; private set; }
public string CountryIso3166 { get; private set; }
public GeoLookupResult(string locationType, string country, string countryIso3166)
{
this.LocationType = locationType;
this.Country = country;
this.CountryIso3166 = countryIso3166;
}
}
But then it seems I can't use LINQ to Objects to create an object with constructors like this (because "GeoLookupResult does not contain a definition for 'locationType'" etc.):
XDocument document;
document = XDocument.Load("http://ift.tt/1ndQyCx");
var query = from i in document.Descendants("response")
select new GeoLookupResult
{
locationType = (string)i.Element("location").Element("type"),
country = (string)i.Element("location").Element("country"),
countryIso3166 = (string)i.Element("location").Element("country_iso3166")
};
Is there a way that I can use the constructors like this? Or should I scratch the idea of immutability and just have public property setters and use those within the LINQ query? (EG: LocationType = (string)i.Element("location").Element("type")
No comments:
Post a Comment