foreach循环未显示正确的结果

时间:2019-05-11 20:17:12

标签: c# asp.net-core methods

我的嵌套foreach循环出现问题。我正在尝试使用有关汽车信息(公司,不同汽车型号)的信息,将数据库中的数据填充到我的列表中。我的问题与我的内部循环有关,而无法继续填充列表。

我期望的结果是这样:


"CompanyId": 1,
"CompanyName": "Toyota"
"ParentVehicleId": 2,
"ParentVehicleName": "Camry",
"ChildVehicleId": 4,
"ChildVehicleName":"Camry/Scepter"

"CompanyId": 1,
"CompanyName": "Toyota"
"ParentVehicleId": 4,
"ParentVehicleName": "Crown"
"ChildVehicleId": 0,
"ChildVehicleName":"N/A"

"CompanyId": 12,
"CompanyName": "Hyundai"
"ParentVehicleId": 13,
"ParentVehicleName": "Accent",
"ChildVehicleId": 0,
"ChildVehicleName":"N/A"

etc...


但是我真正得到的只是这两个:

"CompanyId": 1,
"CompanyName": "Toyota"
"ParentVehicleId": 2,
"ParentVehicleName": "Camry",
"ChildVehicleId": 3,
"ChildVehicleName":"Camry/Vista"

"CompanyId": 1,
"CompanyName": "Toyota"
"ParentVehicleId": 2,
"ParentVehicleName": "Camry",
"ChildVehicleId": 4,
"ChildVehicleName":"Camry/Scepter"

这是我的数据库表的一个片段:

  

车辆表

|----------------------------------------------|
| VehicleId    |  ManufactId  | BrandName      |
|----------------------------------------------|
|  1           |    1         | Toyota         |
|----------------------------------------------|
|  2           |    1         | Camry          |
|----------------------------------------------|
|  3           |    2         | Camry/Vista    |
|----------------------------------------------|
|  4           |    2         | Camry/Scepter  |
|----------------------------------------------|
|  5           |    4         | Crown          |
|----------------------------------------------|
|  6           |    5         | Supra          |
|----------------------------------------------|

  

C#代码

 public List<VehicleListModel>> VehicleMethod()
        {

            List<VehicleListModel> vehicleList = new List<VehicleListModel>();

            foreach (var item in companyInfo)
            {
                var parentInfo = _context.VehicleTable.Where(y => item.VehicleId == y.ManufactId).ToList();
                foreach (var item2 in parentInfo)
                {
                    var childInfo = _context.VehicleTable.Where(y => item2.VehicleId == y.ManufactId).ToList();

                    foreach (var item3 in childInfo)
                    {
                    VehicleListModel vehList = new VehicleListModel();

                        //if ChildVehicleId does not exist, 0 & N/A are 
                        //returned
                        vehList.CompanyId = item.VehicleId;
                        vehList.CompanyName = item?.BrandName ?? "N/A";

                        vehicleList.Add(vehList);

                    }
                }
            }
            return vehicleList;
        }


2 个答案:

答案 0 :(得分:2)

问题基本上是数据的连接方式。

让我们以丰田为例:

|----------------------------------------------|----------------|
| VehicleId    |  ManufactId  |    BrandId     | BrandName      |
|----------------------------------------------|----------------|
|  1           |    null      |      1         | Toyota         |
|----------------------------------------------|----------------|
|  2           |    1         |      1         | Camry          |
|----------------------------------------------|----------------|
|  3           |    2         |      1         | Camry/Vista    |
|----------------------------------------------|----------------|
|  4           |    2         |      1         | Camry/Scepter  |

如您所见,Camry模型是版本与公司之间的关系。

当VehicleId为2(来自Camry)时,您将查找ManufactId为2(Vista和Scepter)的记录。

对于日产:

|----------------------------------------------|----------------|
| VehicleId    |  ManufactId  |    BrandId     | BrandName      |
|----------------------------------------------|----------------|
|  9           |    null      |      9         | Nissan         |
|----------------------------------------------|----------------|
|  10          |    9         |      9         | Datsun         |
|----------------------------------------------|----------------|
|  11          |    9         |      9         | Datsun 13T     |

Datsun没有孩子(没有记录的ManufactId等于10)。将Datsun 13 T记录更新为ManufactId 10以查看它。

其余的也一样。

此外,由于您在最里面的foreach循环内混合了列表的对象(而且您从未到达该代码),所以您甚至都没有得到空的对象。

如果数据错误并且您无法对其进行任何处理,则处理这些情况的一种可能方法是使用可用信息生成对象:

....

List<VehicleListModel> vehicleList = new List<VehicleListModel>();

var companies = _context.Where(x => x.ManufactId == null).ToList();

foreach (var company in companies)
{
    var models = _context.Where(y => company.VehicleId == y.ManufactId).ToList();
    if (models.Any())
    {
        foreach (var model in models)
        {
            var versions = _context.Where(y => model.VehicleId == y.ManufactId).ToList();
            if (versions.Any())
            {
                foreach (var version in versions)
                {
                    VehicleListModel vehList = new VehicleListModel();

                    vehList.CompanyId = company.VehicleId;
                    vehList.CompanyName = company?.BrandName ?? "N/A";
                    vehList.ParentVehicleId = model?.VehicleId ?? 0;
                    vehList.ParentVehicleName = model?.BrandName ?? "N/A";
                    vehList.ChildVehicleId = version?.VehicleId ?? 0;
                    vehList.ChildVehicleName = version?.BrandName ?? "N/A";

                    vehicleList.Add(vehList);
                }
            }
            else
            {
                VehicleListModel vehList = new VehicleListModel();

                vehList.CompanyId = company.VehicleId;
                vehList.CompanyName = company.BrandName;
                vehList.ParentVehicleId = model.VehicleId;
                vehList.ParentVehicleName = model.BrandName;
                vehList.ChildVehicleId = 0;
                vehList.ChildVehicleName = "N/A";

                vehicleList.Add(vehList);
            }
        }
    }
    else
    {
        VehicleListModel vehList = new VehicleListModel();

        vehList.CompanyId = company.VehicleId;
        vehList.CompanyName = company.BrandName;
        vehList.ParentVehicleId = 0;
        vehList.ParentVehicleName = "N/A";
        vehList.ChildVehicleId = 0;
        vehList.ChildVehicleName = "N/A";

        vehicleList.Add(vehList);
    }
}

....

此外,如@Yair所建议,您需要将Crown更改为ManufactId = 1

答案 1 :(得分:0)

您将在每次迭代中覆盖vehList实例。相反,您应该将其初始化移到最内层的循环中,以便在每次迭代中将一个新实例添加到列表中:

        foreach (var item in companyInfo)
        {
            var parentInfo = _context.VehicleTable.Where(y => item.VehicleId == y.ManufactId).ToList();
            foreach (var item2 in parentInfo)
            {
                // This should be removed from the code:
                // VehicleListModel vehList = new VehicleListModel();

                var childInfo = _context.VehicleTable.Where(y => item2.VehicleId == y.ManufactId).ToList();

                foreach (var item3 in childInfo)
                {
                    // Instead, it's initialized here:
                    VehicleListModel vehList = new VehicleListModel();

                    //if ChildVehicleId does not exist, 0 & N/A are 
                    //returned
                    vehList.CompanyId = item.VehicleId;
                    vehList.CompanyName = item?.BrandName ?? "N/A";
                    vehList.ParentVehicleId = item2?.VehicleId ?? 0;
                    vehList.ParentVehicleName = item2?.BrandName ?? "N/A";
                    vehList.ChildVehicleId = item3?.VehicleId ?? 0;
                    vehList.ChildVehicleName = item3?.BrandName ?? "N/A";

                    vehicleList.Add(vehList);

                }
            }
        }