简体   繁体   English

从列表创建嵌套模型

[英]Create nested model from List

I have an existing class 我有一堂课

    public class Employee
{
    public int? EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public int? LocationId { get; set; }
    public string LocationName { get; set; }
    public int? DesignationId { get; set; }
    public string DesignationName { get; set; }
}

Code for fetching data from database: 用于从数据库中获取数据的代码:

using (var ds = new EmployeeDS())
                {
                    using (var dataAdapter = new DataSet.EmployeeDSTableAdapters.EmployeeTableAdapter())
                    {
                    dataAdapter.FillEmployee(ds.Employee,Id);
                      var result = (from DataRow row in ds.Employee.Rows
                                  select new Employee
                                  {
                                      EmployeeId = (row["EmployeeID"] == DBNull.Value) ? null : (int?)row["EmployeeID"],
                                      EmployeeName = (row["EmployeeName"] == DBNull.Value) ? string.Empty : (string)row["EmployeeName"],
                                      LocationId = (row["LocationId"] == DBNull.Value) ? null : (int?)row["LocationId"],
                                      LocationName = (row["LocationName"] == DBNull.Value) ? string.Empty : (string)row["LocationName"],
                                      DesignationId = (row["DesignationId"] == DBNull.Value) ? null : (int?)row["DesignationId"],
                                      DesignationName = (row["DesignationName"] == DBNull.Value) ? string.Empty : (string)row["DesignationName"],
                                  }).ToList();
                    }
                }

Its working fine.But this returning multiple rows for a employee having multiple location or designation.So I need to return the data as nested model like : 它的工作正常,但是对于具有多个位置或指定的员工返回多行,因此我需要将数据作为嵌套模型返回,例如:

public class EmployeeNested
    {
        public int? EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public List<Location> Locations { get; set; }
        public List<Designation> Designations { get; set; }
    }
    public class Location
    {
        public int? LocationId { get; set; }
        public string LocationName { get; set; }
    }
    public class Designation
    {   
     public int? DesignationId { get; set; }
     public string DesignationName { get; set; }
    }

I am using this code for returning nested model: 我正在使用以下代码返回嵌套模型:

 var tempList=result.GroupBy(x => x.EmployeeId, (key, g) => g.OrderBy(e => e.EmployeeId).First());
                    foreach (var item in tempList)
                    {
                        item.Designations = result
                                          .Where(x => x.EmployeeId== item.EmployeeId)
                                          .Select(x => new Designation
                                                 {
                                                  DesignationId = x.DesignationId,
                                                  DesignationName = x.DesignationName
                                                 }).ToList();
                        item.Locations = result
                                          .Where(x => x.EmployeeId== item.EmployeeId)
                                          .Select(x => new Location
                                                 {
                                                  LocationId = x.LocationId,
                                                  LocationName = x.LocationName
                                                 }).ToList();
                    }

Question: 题:

  • Is there any better solution to convert a flat list to nested List?. 有没有更好的解决方案将平面列表转换为嵌套列表?

  • Is it possible to create a generic method for converting flat list to nested list?. 是否可以创建将平面列表转换为嵌套列表的通用方法? So that I can reuse it for another functions too. 这样我也可以将其重用于其他功能。

  • Is it possible to create a nested list directly from dataset? 是否可以直接从数据集创建嵌套列表?

I'm sure there is a good solid pattern for doing this, I just can't find it. 我敢肯定有一个很好的方法可以做到这一点,我只是找不到。

I don't think there is a way to create a nested list from a dataset other than looping through the results and some programming logic. 除了遍历结果和某些编程逻辑之外,我认为没有一种方法可以从数据集中创建嵌套列表。 If your using a relational database I would recommend using entity framework which would generate something similar to your EmployeeNested class once you have your database properly configured. 如果您使用关系数据库,我建议您使用实体框架,一旦正确配置数据库,该实体框架将生成类似于EmployeeNested类的内容。

Is there any better solution to convert a flat list to nested List?. 有没有更好的解决方案将平面列表转换为嵌套列表?

As per my knowledge better solution is to create utility methods which take input list and processed it into another type of list. 据我所知,更好的解决方案是创建实用程序方法,该方法接受输入列表并将其处理为另一种类型的列表。 Please refer below sample code. 请参考下面的示例代码。

public static List<EmployeeNested> ProcessEmployeeData(List<Employee> result)
        {
            return result.Where(x => x.EmployeeId.HasValue).GroupBy(x => x.EmployeeId.Value).Select(x => new EmployeeNested
            {
                EmployeeId = x.Key,
                EmployeeName = x.First().EmployeeName,
                Locations = x.Select(s => new Location
                {
                    LocationId = s.LocationId,
                    LocationName = s.LocationName
                }).ToList(),
                Designations = x.Select(s => new Designation
                {
                    DesignationId = s.DesignationId,
                    DesignationName = s.DesignationName
                }).ToList()
            }).ToList();
        }

Is it possible to create a generic method for converting flat list to nested list?. 是否可以创建将平面列表转换为嵌套列表的通用方法? So that I can reuse it for another functions too. 这样我也可以将其重用于其他功能。

We can't make generic methods unless if there is common property which inherits all property, and how that generic method knows that in which collection need to add some specific object. 除非存在继承所有属性的通用属性,以及该通用方法如何知道在哪个集合中需要添加一些特定对象,否则我们无法创建通用方法。 If there is the way then it going to be complex to implement. 如果有办法的话,实施起来会很复杂。

Is it possible to create a nested list directly from dataset? 是否可以直接从数据集创建嵌套列表?

As per my understanding, we can't make nested collection directly from a dataset, because dataset has no knowledge about in which type of collection we want to bind data which is fetched from the database. 根据我的理解,我们不能直接从数据集中进行嵌套收集,因为数据集不知道我们要绑定从数据库中获取的数据的收集类型。 if you want to make direct collection then there must be an underlying structure which query database according to our model and bind, that is how ORM framework works like entity framework. 如果要进行直接收集,则必须有一个基础结构,可以根据我们的模型查询数据库并进行绑定,这就是ORM框架像实体框架那样工作的方式。

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

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