【问题标题】:Create nested model from List从列表创建嵌套模型
【发布时间】:2016-07-08 20:38:17
【问题描述】:

我有一个现有的类

    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; }
}

从数据库中获取数据的代码:

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();
                    }
                }

它工作正常。但这会为具有多个位置或指定的员工返回多行。所以我需要将数据作为嵌套模型返回,例如:

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; }
    }

我正在使用此代码返回嵌套模型:

 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();
                    }

问题:

  • 有没有更好的解决方案将平面列表转换为嵌套列表?

  • 是否可以创建一个通用方法将平面列表转换为 嵌套列表?这样我也可以将它重用于其他功能。

  • 是否可以直接从数据集创建嵌套列表?

我确信有一个很好的实体模式可以做到这一点,我只是找不到它。

【问题讨论】:

    标签: c# .net linq generics linq-to-objects


    【解决方案1】:

    我认为除了循环遍历结果和一些编程逻辑之外,没有其他方法可以从数据集创建嵌套列表。如果您使用关系数据库,我建议您使用实体框架,一旦您正确配置了数据库,该框架将生成类似于您的 EmployeeNested 类的内容。

    【讨论】:

    • 没有。表之间没有关系。而且在整个项目中我都使用了数据集。那么我所做的是实现嵌套列表的正确方法?
    【解决方案2】:

    有没有更好的解决方案将平面列表转换为嵌套列表?

    据我所知,更好的解决方案是创建实用方法,将输入列表处理成另一种类型的列表。请参考下面的示例代码。

    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();
            }
    

    是否可以创建一个通用方法将平面列表转换为 嵌套列表?这样我也可以将它重用于其他功能。

    我们不能创建泛型方法,除非有继承所有属性的公共属性,以及该泛型方法如何知道需要在哪个集合中添加一些特定对象。如果有方法,那么实现起来会很复杂。

    是否可以直接从数据集创建嵌套列表?

    根据我的理解,我们不能直接从数据集中进行嵌套集合,因为数据集不知道我们要绑定从数据库中获取的数据的集合类型。如果你想直接收集,那么必须有一个底层结构,根据我们的模型查询数据库并绑定,这就是ORM框架像实体框架一样的工作方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2017-07-14
      • 2022-01-11
      • 1970-01-01
      • 2016-11-25
      • 2020-10-22
      相关资源
      最近更新 更多