6

私の MVC3 アプリケーションでは、次のようなフィールド名を持つ匿名コレクションを作成したいと考えています。

new 
{                                                                            
   Buyer.Firstname = "Jim",
   Buyer.Lastname = "Carrey",
   Phone = "403-222-6487",
   PhoneExtension = "",
   SmsNumber = "",
   Buyer.Company = "Company 10025",
   Buyer.ZipCode = "90210",
   Buyer.City = "Beverly Hills",
   Buyer.State = "CA",
   Buyer.Address1 = "Address 10025"
   Licenses[0].IsDeleted = "False",
   Licenses[0].ID = "6",
   Licenses[0].AdmissionDate = "2,1999",
   Licenses[0].AdmissionDate_monthSelected = "2",                                                                           

                                                                    } 

アプリの統合テスト中にカスタム投稿リクエストを送信するために、これが必要です。このフィールド名で匿名コレクションを宣言するにはどうすればよいですか?

4

3 に答える 3

9

次のように、匿名オブジェクトの匿名コレクションを使用します。

Licenses = new [] {
    new {
        IsDeleted = "False",
        ID = "6",
        AdmissionDate = "2,1999",
        AdmissionDate_monthSelected = "2"
    } //, ... and so on
}

...そして文脈では: ([編集] ああ、そして私はあなたのバイヤーを見ませんでした...)

new 
{    
    Buyer = new {
        Firstname = "Jim",
        Lastname = "Carrey",
        Company = "Company 10025",
        ZipCode = "90210",
        City = "Beverly Hills",
        State = "CA",
        Address1 = "Address 10025",
    },                                                                        
    Phone = "403-222-6487",
    PhoneExtension = "",
    SmsNumber = "",
    Licenses = new [] {
        new {
            IsDeleted = "False",
            ID = "6",
            AdmissionDate = "2,1999",
            AdmissionDate_monthSelected = "2"
        }
    }
}
于 2012-10-01T13:20:24.100 に答える
6

オブジェクトとコレクションの初期化構文を使用できます。

var anonymousObject = new 
{                                                                            
    Phone = "403-222-6487",
    PhoneExtension = "",
    SmsNumber = "",
    Buyer = new 
    {
        Firstname = "Jim",
        Lastname = "Carrey",
        Company = "Company 10025",
        ZipCode = "90210",
        City = "Beverly Hills",
        State = "CA",
        Address1 = "Address 10025"
    },
    Licenses = new[]
    {
        new
        {
            IsDeleted = "False",
            ID = "6",
            AdmissionDate = "2,1999",
            AdmissionDate_monthSelected = "2",                                                                           
        }
    }
} 
于 2012-10-01T13:23:01.340 に答える
2

これを試して:

var x = new {
    Phone = "403-222-6487",
    PhoneExtension = "",
    SmsNumber = "",
    Buyer = new {
        Firstname = "Jim",
        Lastname = "Carrey",
        Company = "Company 10025",
        ZipCode = "90210",
        City = "Beverly Hills",
        State = "CA",
        Address1 = "Address 10025"
    },
    Licenses = new[] {
        new {
            IsDeleted = "False",
            ID = "6",
            AdmissionDate = "2,1999",
            AdmissionDate_monthSelected = "2"},
        new {
            IsDeleted = "True",
            ID = "7",
            AdmissionDate = "17,2001",
            AdmissionDate_monthSelected = "3"}
    }
};

注:購入者にはネストされた匿名型を使用し、ライセンスにはさらに別の匿名型のネストされた配列を使用しています。これにより、次のような値にアクセスできます

string name = x.Buyer.Lastname;
string id = x.Licences[0].ID;
于 2012-10-01T13:26:39.600 に答える