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

前端 未结 4 1526
忘了有多久
忘了有多久 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: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 { 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 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.

提交回复
热议问题