我建设,将提供给第三方的,也用自己的Web应用程序/ s的网络API项目。在Web API方法将返回JSON重新$ P $复杂类型/对象psentations。这是pre-定义的类,我可以提供给第三方的,使他们了解数据是结构化和反序列化可以在JSON。我将把这些类作为DTO类,直到有人纠正我。
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 of complex types/objects. These are pre-defined classes which I can make available to third-party's so they understand how the data is structured and can deserialize the JSON. I'll refer to these classes as DTO classes until someone corrects me.
我有以下的自动生成的实体模型(从数据库)以及这是User类反正有关系到扫描表(关系可以忽略这个问题的目的)......
I have the following auto-generated entity model (from database) well this is the User class anyway with a relationship to the Scan table (relationship can be ignored for the purpose of this question)...
public partial class User
{
public User()
{
this.Scans = new HashSet<Scan>();
}
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool Active { get; set; }
public virtual ICollection<Scan> Scans { get; set; }
}
和我想回到presentation层列表如下DTO类的(我不认为我应该使用IEnumerable的业务,以presentation?)。
And the following DTO class which I would like to return to the presentation layer as List (I don't think I should use IEnumerable for business to presentation?).
public class User
{
public int Id;
public string Username;
public string Password;
public bool Active;
}
我的业务层,该组件目前看起来像下面。使用LINQ to从数据库获取用户,然后分析结果并返回一个列表&LT;> POCO用户
My business layer for this component currently looks like below. Using Linq to get users from the database and then parsing the results and returning a List<> of POCO Users.
public List<User> Get()
{
List<User> userList = new List<User>();
using (var db = new MyContext())
{
var query = from u in db.Users
orderby u.FirstName
select u;
foreach (var item in query)
{
User user = new User();
user.Id = item.pkUser;
user.Username = item.Username;
user.Password = item.Password;
user.Active = item.Active;
userList.Add(user);
}
}
return userList;
}
通过解析的结果是这样似乎效率不高,我看到,你可以做这样的事情在LINQ查询没有迭代(例如,在这个问题的映射LINQ查询结果到DTO类)。
我不能确定在哪里/我应该如何实现IEnumerable在我的对象使这种类型的LINQ查询,而且我也不能确定如何编写一个类似于在引用上述问题的查询,但我多少简单的场景。
I'm unsure as to where/how I should implement IEnumerable to enable this type of Linq query on my objects, and am also unsure of how to write a query similar to the one in the referenced question above but for my much simpler scenario.
在此先感谢您的帮助。
P.S。请忽视的事实是,我通过Web API暴露用户名和密码,同时,我返回所有用户的一次。以上是我的code为这一问题的目的的简化版本。
P.S. Please ignore the fact that I am exposing Username and Password via Web API and also that I am returning all Users at once. Above is a simplified version of my code for the purpose of this question.
完整的方法可以是:
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();
}
}
你说你想要的结果不重复。使用LINQ也不排除迭代。你是不是做在你的code,但是当你把它真的发生了了ToList()
方法。