将list <ClsObj1>转换为List <匿名类型obj>,其中ClsObj1具有List <ClsObj2> C#

时间:2019-09-06 17:51:53

标签: c#

我必须向WebApi端点发送一个List<DTOs>,该端点用于以表格格式在UI中呈现该端点。客户端正在请求类似于List<anonimous type obj>的输出。

为此,我正在尝试将list<ClsObj1>转换为List<anonimous type obj>的{​​{1}}到ClsObj1

我尝试了相同的搜索,但找不到合适的解决方案。

这是我的班级模型DTO

List<ClsObj2>

这是上述DTO的JSON O / P数据

public class SPCRawDataDTO
{
    public DateTime ShiftDate { get; set; }
    public string ShiftName { get; set; }
    public string WorkCode { get; set; }
    public string WorkDesc { get; set; }
    public string GroupType { get; set; }
    public List<SPCDynamicSpecValues> SpecValues { get; set; }
    public string Remarks { get; set; }
}

public class SPCDynamicSpecValues
{
    public string PointName { get; set; }
    public decimal SpecValue { get; set; }
} 

下面是我想要的表格格式,与上面的json O / P相同,

[
    {
        "shiftDate": "2019-08-30T00:00:00",
        "shiftName": "Shift A",
        "workCenterCode": "04INDLGC",
        "workCenterDesc": "CASTING",
        "groupType": "Material",
        "specValues": [
            { "pointName": "W1", "specValue": 242.000 },
            { "pointName": "W2", "specValue": 234.000 },
            { "pointName": "W3", "specValue": 250.000 },
            { "pointName": "W4", "specValue": 236.000 },
            { "pointName": "W5", "specValue": 248.000 } ],
        "remarks": "OK"
    },
    {
        "shiftDate": "2019-08-30T00:00:00",
        "shiftName": "Shift A",
        "workCenterCode": "WCode",
        "workCenterDesc": "WDesc",
        "groupType": "Material",
        "specValues": [
            { "pointName": "W1", "specValue": 238.000 },
            { "pointName": "W2", "specValue": 230.000 },
            { "pointName": "W3", "specValue": 246.000 },],
        "remarks": "OK"
    },
    {
        "shiftDate": "2019-08-30T00:00:00",
        "shiftName": "Shift A",
        "workCenterCode": "WCode",
        "workCenterDesc": "WDesc",
        "groupType": "Material",
        "specValues": [
            { "pointName": "W2", "specValue": 246.000 },
            { "pointName": "W3", "specValue": 232.000 },
            { "pointName": "W4", "specValue": 238.000 },
            { "pointName": "W5", "specValue": 230.000 },
            { "pointName": "W6", "specValue": 244.000 } ],
        "remarks": "OK"
    }
]

OutPut JSON格式或多或少(因为上面显示的W1至W6可能类似于“ P1”,“ P5”,“ P9”)

sDate     | sName   | wCode | wDesc | grpType | W1 | W2 | W3 | W4 | W5 | W6 |
2019-08-30| Shift A | WCode | WCode | GType   |230 |240 |343 |343 |356 | 0  |
2019-08-30| Shift A |WCode  |WCode  |GType    |230 |240 |343 | 0  |  0 |  0 |
2019-08-30| Shift A |WCode  |WCode  |GType    |0   |240 |343 |343 |356 |356 |

因此,我必须格式化{ "shiftDate": "2019-08-30T00:00:00", "shiftName": "Shift A", "workCenterCode": "WCode", "workCenterDesc": "WDesc", "groupType": "GType", "W1": 242.000 "W2": 234.000 "W3": 250.000 "W4": 236.000 "W5": 248.000 "remarks": "OK" } DTO,以使输出应类似于UI表中的上述格式。

现在如何将List<SPCRawDataDTO>本身具有List<SPCRawDataDTO>的{​​{1}}转换为SPCRawDataDTO

1 个答案:

答案 0 :(得分:0)

您可以使用ExpandoObject将每个specValue作为属性动态添加到结果对象,例如:

private static object ToObj(SPCRawDataDTO src)
{
    var dest = new ExpandoObject() as IDictionary<string, object>;
    dest.Add("ShiftDate", src.ShiftDate);
    dest.Add("ShiftName", src.ShiftName);
    // TODO: add all other properties
    for (var i = 0; i < src.SpecValues.Count; ++i)
    {
        dest.Add($"W{i + 1}", src.SpecValues[i].SpecValue);
    }
    return dest;
}

可以通过以下方式调用:

List<SPCRawDataDTO> dtos = ... // TODO: the dtos
var result = dtos.Select(ToObj);