Return anonymous type from LINQ query?

前端 未结 2 1945

In that in business logic layer, I have created a class like this:

public List SelectAllData()
{
    try
    {
        return (from obj i         


        
相关标签:
2条回答
  • If you return RoleHasRight you need to construct it, not an anonymous type as you do now:

    select new RoleHasRight
    {
        obj,
        role.roleName
    }).ToList();
    
    0 讨论(0)
  • 2021-01-17 02:00

    You are creating a new, anonymous Type by using:

    select new { obj,role.roleName}
    

    This type has 2 properties (the rolename and the roleHasRights object). In order to be able to return the same inforamtion by the method, you need to create a class with this information, or to return a dynamic.

    e.g.

    public class RoleHasRightInfo {
       public string Rolename {get;set;}
       public roleHasRight Right {get;set;}
    
    }
    

    and create it within the linq expression like this:

    select new RoleHasRightInfo(){ Right=obj,Rolename = role.roleName}
    

    The method would then need to return a List of this new type:

    public List<RoleHasRightInfo> SelectAllData() {
    ...
    }
    
    0 讨论(0)
提交回复
热议问题