2017-05-09 4 views
-1

私はItemという名前のクラスを持っていて、それはAirpricepointのリストを保持しています。私は、クラスのすべてのプロパティを作成して初期化し、Airpricepointリストを追加することができます。 Airpricepointクラスには、いくつかのプロパティを持つ別のリストAirPricingInfoがあります。 AirPricingInfoのメンバーにアクセスして初期化し、リストに追加するにはどうすればよいですか?リストコレクション内のリストを初期化する方法

public class Item 
{ 
    public List<Airpricepoint> AirPricePoint { get; set; } 
} 

public class Airpricepoint 
{ 
    public List<Airpricinginfo> AirPricingInfo { get; set; } 
    public string AirPricingResultMessage { get; set; } 
    public string FeeInfo { get; set; } 
    public string FareNote { get; set; } 
    public string TaxInfo { get; set; } 
    public string Key { get; set; } 
    public string TotalPrice { get; set; } 
    public string BasePrice { get; set; } 
    public string ApproximateTotalPrice { get; set; } 
    public string ApproximateBasePrice { get; set; } 
    public string EquivalentBasePrice { get; set; } 
    public string Taxes { get; set; } 
    public string Fees { get; set; } 
    public string Services { get; set; } 
    public string ApproximateTaxes { get; set; } 
    public string ApproximateFees { get; set; } 
    public string CompleteItinerary { get; set; } 
} 

public class Airpricinginfo 
{ 
    public object FareInfo { get; set; } 
    public object FareStatus { get; set; } 
    public IList<FareInfoRef> FareInfoRef { get; set; } 
    public object BookingInfo { get; set; } 
    public IList<TaxInfo> TaxInfo { get; set; } 
    public string FareCalc { get; set; } 
} 

var lowfaresearchres = new Lowfaresearchres(); 
lowfaresearchres.Items= new Item(); 
lowfaresearchres.Items.AirPricePoint = new List<Airpricepoint>();  
lowfaresearchres.Items.AirPricePoint.Add(new Airpricepoint() 
    { 
     ApproximateBasePrice = airPricePoint.ApproximateBasePrice, 
     ApproximateTotalPrice = airPricePoint.ApproximateTotalPrice, 
     ApproximateTaxes = airPricePoint.ApproximateTaxes, 
     ApproximateFees = airPricePoint.Fees, 
     BasePrice = airPricePoint.BasePrice, 
     Taxes = airPricePoint.Taxes, 
     TotalPrice = airPricePoint.TotalPrice 
    }); 
+1

をどのようにあなたは知っていますかリスト内のアイテムにアクセスしますか?そうでない場合は、C#/ .NETの基本的な/基本的なコレクションに関するC#チュートリアルを参照してください。 – elgonzo

+0

'Item'コンストラクタで' List 'をインスタンス化することもできますそこから。 – maccettura

答えて

0

私が何を求めていることは二つの部分だと思う - (Addを呼び出さず)の項目でリストを初期化する方法、およびどのように(実際には同じ答えである)他のリストを含むリストを初期化します。

任意のオブジェクトをインラインで初期化するための構文はカーリーを使用することですがnew ObjectType後、あなたがPropertyName = Valueペアのコンマ区切りのリストを持っている括弧内{}ブレース。

単一プロパティを持つクラスを使用して、この単純な形態は次のようになります単一のリストを使用して、この

var person = new Person { Name = "Henry", Age = 25 }; 

単純な形式である:ネストされたリストを

var stringList = new List<string> { "one", "two", "three" }; 

var people = new List<Person> 
{ 
    new Person {Birthday = DateTime.Now, Name = "Jack"}, 
    new Person {Birthday = DateTime.Parse("1/1/1980"), Name = "Jane"} 
}; 

、それがあろう

var listOfListOfStrings = new List<List<string>> 
{ 
    new List<string>{ "one", "two", "three" }, 
    new List<string>{ "four", "five", "six" } 
}; 

オブジェクトを入力するには、いくつかの方法があります。

// Create child lists 
IList<FareInfoRef> fareInfoRef = IList<FareInfoRef>(); // And add some items 
IList<TaxInfo> taxInfo = new IList<TaxInfo>(); // And add some items 

// Create the parent and add children 
Airpricinginfo airPricingInfo = new Airpricinginfo 
{ 
    TaxInfo = taxInfo, 
    FareInfoRef = fareInfoRef, 
    // initialize other properties 
} 

// Continue with this pattern. . . 

しかし、私はあなたが求めていると思うことは、あなたがそれをすべてのインライン(可能性があります行うことができますどのように、次のとおりです。一つは、あなたがそれらを作成するよう親にそれらを追加し、最初の最も内側のリストを作成し、初期化することですちょっと面倒だが、より簡潔に見えるかもしれない)。

だからあなたのオブジェクトにこの概念を適用し、あなたがこれに似て何か(私はこれをコンパイルしていなかった注意ので、どこかのマイナーなエラーがあるかもしれない)だろう:

lowfaresearchres.Items.AirPricePoint = new List<Airpricepoint> 
{ 
    new Airpricepoint 
    { 
     AirPricingInfo = new List<Airpricinginfo> 
     { 
      new Airpricinginfo 
      { 
       TaxInfo = new IList<TaxInfo>(), 
       FareInfoRef = IList<FareInfoRef>() 

       // Add other AirPricingInfo properties 
      }, 
      // Add more Airpricinginfo objects separated by commas 
     } 

     // Add other Airpricepoint properties 
    }, 
    // Add more Airpricepoint objects separated by commas 
}; 
+0

非常に素晴らしい説明ありがとうたくさん – Muhammed

関連する問題