Return anonymous type from LINQ query?

前端 未结 2 1944

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

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


        
2条回答
  •  别那么骄傲
    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 SelectAllData() {
    ...
    }
    

提交回复
热议问题