2012-10-01 162 views
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",                   

                    } 

我想有這樣的爲了我的應用程序的集成測試期間發送自定義POST請求。我怎樣才能用這個字段名稱來聲明一個匿名集合?

+0

使用嵌套的匿名類型。任何房產名稱均不允許使用期限。除非你想將它轉換成字典... –

+3

我也會注意到它非常鼓舞**你爲此創建了一個明確的視圖模型,而不是使用匿名類型。將來使用匿名類型維護代碼將會很困難。 –

+0

出於好奇,爲什麼'Buyer'的'Phone','PhoneExtension'和'SmsNumber'屬性?整個對象不是「買方」? – canon

回答

7

使用匿名對象的匿名收集,就像這樣:

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" 
     } 
    } 
} 
+0

可能希望用'new {...}包裝上述內容以使其更清晰 –

+0

這會有所幫助,但在綁定服務器上的集合時,我仍然收到許可證的空集合。但我覺得我是正確的。謝謝! –

+0

可能發生這種情況是因爲匿名列表無法序列化 –

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",                   
     } 
    } 
} 
+0

+1。基本上與我的解決方案相同。 –

+0

@ OlivierJacot-Descombes除了他以3.5分鐘的速度擊敗你...;) – canon

+0

@canon:當你在Visual Studio中工作時,可能會發佈一個答案。 –

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;