【问题标题】:Copy custom list value to var or dynamic and loop through? [closed]将自定义列表值复制到 var 或 dynamic 并循环遍历? [关闭]
【发布时间】:2011-06-22 19:38:35
【问题描述】:

在我的 objectdatasource 中,我使用 _selected 事件从对象返回的列表中获取一些值。

所以我使用e.Returnvalue

protected void ObjTrailerList_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {

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

现在我想将整个自定义列表值复制到 var 或动态 n 迭代。 怎么做?我不想创建 MovieTrailers 对象列表并复制其中的值。

我的自定义列表是

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

【问题讨论】:

  • 为什么-1票???我问错了什么???
  • 您没有提供足够的细节,或向我们展示您想要做什么。解决这个问题,也许关闭投票会停止。
  • 哦...我尝试的操作给了我错误,所以我没有发布代码。我可以选择创建我不想创建的 List。我会发布我正在尝试的。我发布了我如何复制到 var 或 dymaic..现在我不知道如何遍历它们。
  • 我确实接受了答案,但我没有得到确切的答案,所以我没有接受答案。

标签: c# list c#-4.0 objectdatasource


【解决方案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 }

【讨论】:

  • 感谢 Jalal...它有效..我必须根据需要进行修改..非常感谢.s
  • @Abhishek:不客气 :)
猜你喜欢
  • 2020-12-10
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 2023-03-12
  • 2011-02-28
  • 2023-03-09
  • 1970-01-01
  • 2013-04-21
相关资源
最近更新 更多