简体   繁体   English

可查询列表<dto>转换</dto>

[英]IQueryable List<DTO> conversion

I need to fill an IQueryable of UserDTO with a List of TeamsDTO without a foreach-loop or materializing the data.我需要使用 TeamsDTO 列表填充 UserDTO 的 IQueryable,而无需使用 foreach 循环或物化数据。

I have the following classes:我有以下课程:

public class User
{
    public int Id {get;set;}
    public string Name {get;set;}
    public List<Team> Teams {get;set;}
}

public class Team
{
    public int Id {get;set;}
    public string Teamname {get;set;}
    public string Description {get;set;}
}

And the following DTOs:以及以下 DTO:

public class UserDTO
{
    public int Id {get;set;}
    public List<TeamDTO> TeamsDTOList{get;set;}
}

public class TeamDTO
{
    public int Id {get;set;}
    public string Teamname {get;set;}
}

And the Code so far:到目前为止的代码:

public static IQueryable<UserDTO> GetQueryableUserDTO(IQueryable<User> query)
        {
            var retQueryable = Enumerable.Empty<UserDTO>().AsQueryable();
            try
            {
                retQueryable = query.AsEnumerable()
                                .Select(k => new UserDTO
                                {
                                    Id = k.Id,
                                    TeamsDTOList= new List<TeamDTO>()
                                    {
                                        //Can't figure out this part
                                    }
                                });
            }
            catch (Exception ex)
            {
                BaseManager.Write2Log(ex);
            }
            return retQueryable;
        }

Thanks for the help in advance.我在这里先向您的帮助表示感谢。

PS: Classes/code are of course a simplified example. PS:类/代码当然是一个简化的例子。

What about this:那这个呢:

retQueryable = query.AsEnumerable().Select(k => new UserDTO()
                {
                    Id = k.Id,
                    TeamsDTOList = k.Teams.Select(t => new TeamDTO(){ Id = t.Id, Teamname = t.Teamname}).ToList()
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM