WebApi:将列表<int>变量正确放入对象类型的属性中



显然,我有一个object类型的模型属性,可以从任何类型的值输出。我的任务是把那里的List<int>值,但当我把它作为,或者,例如,作为list.Cast<object>(),我得到数组的数组,如[[1], [2], [255]]在这个模型字段JSON序列化后控制器输出。

重要的是,我不能改变属性类型。任何建议吗?

下面是这个属性的代码。DocumentChildIds,BoolValue等是该模型类中的其他属性:

public object ResolvedValue =>
FieldType == CustomFieldType.Document ? DocumentChildIds :
FieldType == CustomFieldType.Boolean ? BoolValue :
FieldType == CustomFieldType.Date ? DateValue.HasValue ? DateValue.Value.ToShortDateString() : "" :
FieldType == CustomFieldType.Integer ? IntValue :
FieldType == CustomFieldType.DB_LOOKUP 
|| FieldType == CustomFieldType.LOOKUP
|| FieldType == CustomFieldType.MULTI_LOOKUP
|| FieldType == CustomFieldType.LOOKUP_TO_CLIENT ? (object) LookupId : StringValue; //by default all the rest types having string value

其中DocumentChildIds具有List<int>类型,并且在输入{1,2,3}时,我在Json中获得[[1],[2],[3]]而不是[1,2,3]

我错了。这个属性值在解析了ToList()调用它的位置后通过Group by,并成为数组的数组。

var arrayValueFields = documentComposite
.Where(i => ArrayValueResultTypes.Contains(i.FieldType))
.GroupBy(i => i.FieldId)
.Select(g => new DocumentItemFieldValueDto
{
FieldId = g.Key,
FieldType = g.Select(i => i.FieldType).FirstOrDefault(),
FieldCaption = g.Select(i => i.FieldCaption).FirstOrDefault(),
FieldValue = g.Select(i => i.ResolvedValue).ToList()
...
}

不要重复我的错误

最新更新