我通過解析XDocument的值創建一個LINQ對象。我的理解是對象應該創建爲不可變的,除非您確實需要稍後更改這些值,所以我已經創建了私人設置器。C# - 如何創建一個LINQ到對象的不可變對象
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;
}
}
但後來似乎我不能使用LINQ到對象來創建這樣構造一個對象(因爲「GeoLookupResult不包含‘的locationType’的定義」等):
XDocument document;
document = XDocument.Load("http://api.wunderground.com/api/d36f54198ebbb48c/geolookup/q/England/London.xml");
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")
};
有沒有一種方法可以使用這樣的構造函數?或者我應該抓住不變性的想法,只是有公共屬性設置器並使用LINQ查詢中的那些? (EG:LocationType = (string)i.Element("location").Element("type")
嗨,這工作表示感謝。我沒有意識到可以在查詢中使用構造器。 – user9993 2014-08-31 09:53:19
如果此答案有助於解決您的問題,請考慮將其標記爲接受的答案(左側爲綠色勾號)。謝謝! – 2014-09-01 05:36:00