将自定义列表值复制到var或动态并循环? [英] Copy custom list value to var or dynamic and loop through?

查看:205
本文介绍了将自定义列表值复制到var或动态并循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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;
}
}


解决方案

p> private static void TestDynamic(动态列表)
{
foreach(列表中的var项)
{
if(item是string)
{
string foo = item; //使用它作为字符串
Console.WriteLine(字符串是:{0},foo);
}
else
{
Console.WriteLine(item);
}
}
}

static void Mian()
{
//传递字符串列表
TestDynamic new List< string> {Foo,Bar,Baz});

//传递匿名类的列表
TestDynamic(new List< dynamic> {new {Age = 25,BirthDay = new DateTime(1986,1,3)}},new {Age = 0,BirthDay = DateTime.Now}});

// TestDynamic(25); //这将导致运行时在foreach行的异常
}


// output:
字符串是:Foo
字符串是:Bar​​
字符串是:Baz
{Age = 25,BirthDay = 3/1/1986 00:00:00}
{Age = 0,BirthDay = 23/6/2011 01:23:18}


In my objectdatasource i am using _selected event to get some value from the list which object returns.

So i am using e.Returnvalue.

protected void ObjTrailerList_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {

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

Now i want to copy entire custom list value to var or dynamic n iterate through. How to do? I don't want to create object List of MovieTrailers and copy the value in it.

my custom list is

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

解决方案

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 }

这篇关于将自定义列表值复制到var或动态并循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆