2011-06-22 44 views
0

在我的objectdatasource中,我正在使用_selected事件從列表中獲取某個對象返回的值。所以我使用e.Returnvalue將自定義列表值複製到var或dynamic並循環遍歷?

protected void ObjTrailerList_Selected(object sender, ObjectDataSourceStatusEventArgs e) 
    { 

     dynamic details = e.ReturnValue; 
     var d = e.ReturnValue;} 

現在我想複製整個自定義列表值VAR或動態ñ遍歷。 怎麼辦?我不想創建MovieTrailer對象列表並複製其中的值。

我的自定義列表是

public class MovieTrailers 
{ 
    public int? TrailerId 
    { 
     get; 
     set; 
    } 
    public string MovieName 
    { 
     get; 
     set; 
    } 
    public string TrailerUrl 
    { 
     get; 
     set; 
    } 
} 
+0

爲什麼-1票????我問什麼錯誤的東西? –

+0

你沒有給出足夠的細節,或向我們展示你想要做什麼。解決這個問題,也許最後的選票將停止。 –

+0

哦...我試過給了我錯誤,所以我沒有發佈代碼。我可以選擇創建我不想要的List 。我會發布我正在嘗試。我發佈如何複製到var或dymaic ..現在我不知道如何遍歷它們。 –

回答

1
private static void TestDynamic(dynamic list) 
{ 
    foreach (var item in list) 
    { 
     if (item is string) 
     { 
      string foo = item;//use it as string 
      Console.WriteLine("The string is: {0}", foo); 
     } 
     else 
     { 
      Console.WriteLine(item); 
     } 
    } 
} 

static void Mian() 
{ 
    //pass a list of strings 
    TestDynamic(new List<string> { "Foo", "Bar", "Baz" }); 

    //pass a list of anonymous class 
    TestDynamic(new List<dynamic> { new { Age = 25, BirthDay = new DateTime(1986, 1, 3) }, new { Age = 0, BirthDay = DateTime.Now } }); 

    //TestDynamic(25);//this will cause exception at run time at the foreach line 
} 


//output: 
The string is: Foo 
The string is: Bar 
The string is: Baz 
{ Age = 25, BirthDay = 3/1/1986 00:00:00 } 
{ Age = 0, BirthDay = 23/6/2011 01:23:18 } 
+0

謝謝賈拉爾......它的工作原理..我必須根據我的需要進行修改..非常感謝。 –

+0

@Abhishek:歡迎您:) –