How can I convert Linq results to DTO class object without iteration

前端 未结 4 1454
忘了有多久
忘了有多久 2020-12-24 14:07

I\'m building a Web API project that will be made available to third-party\'s and also used by my own web application/s. The Web API methods will return JSON representations

相关标签:
4条回答
  • 2020-12-24 14:17

    The complete method could be:

    public List<User> Get()
    {
        using (var db = new MyContext())
        {
            return (from u in db.Users
                    orderby u.FirstName
                    select new User()
                    {
                        Id = u.pkUser,
                        Username = u.Username,
                        Password = u.Password,
                        Active = u.Active
                    }).ToList();
        }
    }
    

    You said you want the result "without iteration". Using LINQ also does not eliminate the iteration. You are not doing it in your code, but it really happens when you call the ToList() method.

    0 讨论(0)
  • 2020-12-24 14:19

    You can use LINQ to Objects to do the conversion:

    using (var db = new MyContext())
    {
        var query = from u in db.Users
                    orderby u.FirstName
                    select u;
    
        return query.AsEnumerable().Select(item => 
                     new User 
                         { 
                            Id = item.pkUser,
                            Username = item.Username,
                            Password = item.Password,
                            Active = item.Active
                         }).ToList();
    }
    
    0 讨论(0)
  • 2020-12-24 14:20

    We can get really compact if we use AutoMapper.

    Bootstrap the mapper in your repository:

    Mapper.CreateMap<AutoGenNamespace.User, DtoNamespace.User>();

    Then it's pretty simple (and there's nothing wrong with returning IEnumerable).

    public IEnumerable<User> Get()
    {
        using (var db = new MyContext())
        {
            return (from u in db.Users
                    orderby u.FirstName
                    select Mapper.Map(u)).AsEnumerable();
        }
    }
    
    0 讨论(0)
  • 2020-12-24 14:33

    In my query I have parent table with multiple childs.

    public class TeamWithMembers
            {
                public int TeamId { get; set; }
                public string TeamName { get; set; }
                public string TeamDescription { get; set; }
                public List<TeamMembers> TeamMembers { get; set; }
            }
    

    In this when I was using the ToList() in the linq query it is giving the error. Now I am using the anonymous type in the linq query and convert it into the next query like this

    List<TeamModel.TeamWithMembers> teamMemberList = teamMemberQueryResult.AsEnumerable().Select(item =>
    new TeamModel.TeamWithMembers() {
        TeamId=item.TeamId,
        TeamName=item.TeamName,
        TeamMembers=item.TeamMembers.ToList()
    }).ToList();
    

    Where teamMemberQueryResult is the resultset from the linq query.

    0 讨论(0)
提交回复
热议问题