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

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

提交回复
热议问题