开发者

Instantiate defined object with Linq Query

I know that you can instantiate anonymous types with Linq but I am looking to instantiate an object I have already defined. Every time I do, all the properties are returned with their defaults (null, 0, etc.) Is there a way to make this work?

I've tried something like this:

ServiceDepartment[] serviceDepartments = (from d in departments
                                          orderby d.department_name
                                          select new ServiceDepartment
                                          {
                                              DepartmentID = d.department_id,
      开发者_JS百科                                        DepartmentName = d.department_name
                                          }).ToArray();


That should work absolutely fine. Do your properties definitely work? I suggest you put breakpoints on them and see whether they get called, and what the values are at that point.

EDIT: Okay, this sounds like it could be a Subsonic issue. I suggest you force the last part to be done in-process using AsEnumerable:

var query  = from d in departments
             orderby d.department_name 
             select new { id = d.department_id, name = d.department_name };

var serviceDepartments = query.AsEnuemrable()
                              .Select(x => new ServiceDepartment
                                      {
                                          DepartmentName = x.name,
                                          DepartmentID = x.id
                                      })
                              .ToArray();


Your code should work. Maybe values are changing in ServiceDepartment or some where else. if you didn't figure it out please post ServiceDepartment code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜