匿名对象和object的转换
有时候经常用到需要把一个匿名对象存入session或List<object>或其他容器中,可是取出来的时候变成object了,不太方便使用。
@foreach (dynamic item in ViewBag.AAA)
    {
            
        <div>@item.GetType().GetProperty("Title").GetValue(item)</div>
        foreach (var cond in item.GetType().GetProperty("NovelCharpter").GetValue(item))
        { 
            <div>@cond.GetType().GetProperty("Name").GetValue(cond)</div>
        }
    }
ViewBag.AAA = (from i in db.Novel.Take(10).AsEnumerable()
                           select new
                           {
                               i.ID,
                               i.Title,
                               i.Author,
                               NovelCharpter = from m in i.NovelCharpter
                                               select new {
                                                    m.ID,
                                                    m.Name
                                               }
                           }).ToList();


 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号