如何初始化列表集合中的列表

时间:2017-05-09 16:23:58

标签: c# c#-4.0 c#-3.0

我有一个名为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 个答案:

答案 0 :(得分:0)

我认为您要问的是两个部分 - 如何使用项目初始化列表(不调用Add),以及如何初始化包含其他列表的列表(实际上是相同的答案) )。

初始化内联任何对象的语法是在{}之后使用curley括号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
};
相关问题