如何将“ Mech”的数据合并为“ Tech”数据?

时间:2019-04-22 04:11:18

标签: c#

我正在尝试将“ MECHANICAL”的数据连接到“ TECHNICAL”,但我不知道该怎么做。

这是我当前的代码,列出了DecodedFactoryOptions下的所有数据: 首先,我将每种设备按其类别分组。

 public static IEnumerable<VehicleFactoryOptionModel> GetFactoryOptions(IEnumerable<DecodedFactoryOption> options)
        {
            var optionEquipment = new List<VehicleFactoryOptionModel>();

            if (options == null) return optionEquipment;

            var optEquip = options.GroupBy(e => e.Category);

            foreach (var equip in optEquip)
            {
                var optionalEquipment = new VehicleFactoryOptionModel { Data = new List<VehicleCategoryOptionModel>()};

                if (equip != null)
                {
                    foreach (var eq in equip)
                    {
                        optionalEquipment.TypeId = eq.OptionHeaderId;
                        optionalEquipment.Description = eq.Description;
                        optionalEquipment.Data.Select(d => d.StyleIds == eq.AppliedStyleId);
                        optionalEquipment.chromeCode = eq.chromeCode;
                        optionalEquipment.oemCode = eq.oemCode;
                        optionalEquipment.optionKindId = eq.optionKindId;
                        if (eq.OptionCategoryId == null) continue;
                        FillOptionCategoryInfo(eq, optionalEquipment);
                    }
                }

                optionEquipment.Add(optionalEquipment);
            }

            return optionEquipment;
        }

以上代码的结果为:

[
    {
        "TypeId": 1236,
        "TypeName": "MECHANICAL",
        "Data": [
            {
                "Description": "Engine: 5.7L V8 DOHC 32V FFV w/i-Force",
                "Categories": [
                    {
                        "Id": 1052,
                        "Name": "Engine-8 Cyl"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },
            {
                "Description": "Engine: 5.7L V8 DOHC 32V FFV w/i-Force",
                "Categories": [
                    {
                        "Id": 1213,
                        "Name": "Fuel System-Flex Fuel"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            }
    },
    {
        "TypeId": 1176,
        "TypeName": "TECHNICAL",
        "Data": [
            {
                "Description": "Tires: P275/55R20 Bridgestone/Dunlop Performance",
                "Categories": [
                    {
                        "Id": 1092,
                        "Name": "Tire-Front-Performance"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },
            {
                "Description": "Tires: P275/55R20 Bridgestone/Dunlop Performance",
                "Categories": [
                    {
                        "Id": 1097,
                        "Name": "Tire-Rear-Performance"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },
    {
        "TypeId": 10689,
        "TypeName": "ENTERTAINMENT",
        "Data": [
            {
                "Description": "Radio: Entune Premium w/JBL Audio & Navigation -inc: AM/FM/HD radio, CD player MP3/WMA playback capability, high-resolution 7\" touch-screen display, auxiliary audio jack, USB 2.0 port, iPod connectivity and control, iTunes Tagging, traffic and weather, JBL speakers and amplifier, Bluetooth hands-free phone capability, phone book access, voice recognition and music streaming, Entune App Suite includes Bing, iHeartRadio, MovieTickets.com, OpenTable, Pandora, Yelp and Facebook Places; real-time info including traffic, weather, fuel prices, sports and stocks, SIRIUSXM Satellite Radio, 90-day free trial",
                "Categories": [
                    {
                        "Id": 1014,
                        "Name": "Audio-AM/FM Stereo"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },
            {
                "Description": "Radio: Entune Premium w/JBL Audio & Navigation -inc: AM/FM/HD radio, CD player MP3/WMA playback capability, high-resolution 7\" touch-screen display, auxiliary audio jack, USB 2.0 port, iPod connectivity and control, iTunes Tagging, traffic and weather, JBL speakers and amplifier, Bluetooth hands-free phone capability, phone book access, voice recognition and music streaming, Entune App Suite includes Bing, iHeartRadio, MovieTickets.com, OpenTable, Pandora, Yelp and Facebook Places; real-time info including traffic, weather, fuel prices, sports and stocks, SIRIUSXM Satellite Radio, 90-day free trial",
                "Categories": [
                    {
                        "Id": 1017,
                        "Name": "Audio-CD Player"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },


我希望将“ MECHANICAL”的数据合并到“ TECHNICAL”数据中。

这是我想要的结果:

{
        "TypeId": 1176,
        "TypeName": "TECHNICAL",
        "Data": [
            {
                "Description": "Tires: P275/55R20 Bridgestone/Dunlop Performance",
                "Categories": [
                    {
                        "Id": 1092,
                        "Name": "Tire-Front-Performance"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },
            {
                "Description": "Tires: P275/55R20 Bridgestone/Dunlop Performance",
                "Categories": [
                    {
                        "Id": 1097,
                        "Name": "Tire-Rear-Performance"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },

                "Description": "Engine: 5.7L V8 DOHC 32V FFV w/i-Force",
                "Categories": [
                    {
                        "Id": 1052,
                        "Name": "Engine-8 Cyl"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            },
            {
                "Description": "Engine: 5.7L V8 DOHC 32V FFV w/i-Force",
                "Categories": [
                    {
                        "Id": 1213,
                        "Name": "Fuel System-Flex Fuel"
                    }
                ],
                "StyleIds": [
                    362074,
                    362107
                ]
            }
    {

1 个答案:

答案 0 :(得分:0)

类似下面的方法会起作用,请记住我没有测试

  // instead of new add the call to get the Ienumerable
  IEnumerable<VehicleFactoryOptionModel> optionModels = new List<VehicleFactoryOptionModel>();
  // fill type name and id for the technical
  var result = optionModels.Select(x => new {x.TypeId, x.TypeName, x.Data}).
      Where(x => x.TypeName.ToString() == "Technical").ToList();

  // select only the data list for mechanical
  var test = optionModels.
      Where(x=>x.TypeName.ToString() == "Mechanical").SelectMany(x=>x.Data );

  // add it to the technical list,
  // assumption that there will be only one list for the techincal..thats why the index 0
  result[0].Data.AddRange(test);