MVC 通过linqtoSQL返回匿名类的集合 View怎么操作?
http://topic.csdn.net/u/20110627/10/cfcf6b45-f8fe-4530-8ef7-12c59d42898c.html
这个帖子里面有表结构
select id,projectname from projects (项目表)
select id,empName,phone from employees (员工表)
select id,projectid,employeeid,projectRote from project_employee (项目与员工的关系表,projectRote 字段是员工在项目角色)
传入一个项目ID(16),返回该项目的参与者和在项目中的角色
用linq如下实现了:
var view = from pe in project_employee
where pe.id == 16
select new { pe.employees.empName, pe.projectRote };
ViewData["pe"]=view.ToList();
但是前台我绑定的时候,发现这view是个匿名类。不知道前台该怎么绑定了
foreach (var item in ViewData["pe"] as List<Project_Employee>)
这样的方式肯定不行了
------解决方案--------------------
var view = from pe in project_employee
where pe.id == 16
select new Project_Employee{ empName=pe.employees.empName, projectRote =pe.projectRote };
ViewData["pe"]=view.ToList();
------解决方案--------------------
一般 都用强类型,不用匿名类型。
C# code
//匿名类型
var view = ....select new { empName=pe.employees.empName, pe.projectRote };
ViewData["pe"]=view.ToList();
//前台
foreach (var item in (IEnumerable<object>)ViewData["pe"])
{
var empName = item.GetType().GetProperty("empName").GetValue(item, null);
var projectRote = item.GetType().GetProperty("projectRote").GetValue(item, null);
}