如何通过FhirClient获取FHIR CodeSystem中Property的属性值?
问题:处理FHIR CodeSystem中Concept的Property数据
我通过以下代码调用NHS Retrieve Reference Data方法:
var result = await fhirClient.ReadAsync<CodeSystem>(url);
返回的JSON片段如下:
"concept": [ { "code": "BOOKED_CLINICAL_NEED", "display": "Booked more urgently due to clinical need", "property": [ { "code": "effectiveFrom", "valueDateTime": "2019-07-23T17:09:56.000Z" }, { "code": "commentIsMandatory", "valueBoolean": true }, { "code": "canCancelAppointment", "valueBoolean": false } ] }
之前处理扩展数据时用过GetExtensionValue方法,但没找到处理Property的类似方法。请问有没有简便方法,还是需要手动转换为所需类型?
解决方案
目前官方FHIR SDK没有提供和GetExtensionValue直接对应的Property处理方法,建议自己封装工具方法来简化操作,避免重复的手动类型转换。
封装通用获取Property值的扩展方法
针对CodeSystem.Concept.Property集合,写一个扩展方法,根据指定code提取对应值并转换为目标类型:
public static class CodeSystemExtensions { public static T GetPropertyValue<T>(this IEnumerable<CodeSystem.ConceptPropertyComponent> properties, string propertyCode) { var property = properties.FirstOrDefault(p => p.Code == propertyCode); if (property == null) throw new KeyNotFoundException($"Property with code '{propertyCode}' not found."); // 按目标类型匹配对应的Value属性 switch (typeof(T)) { case Type t when t == typeof(DateTime): return (T)(object)property.ValueDateTime.Value; case Type t when t == typeof(bool): return (T)(object)property.ValueBoolean.Value; case Type t when t == typeof(string): return (T)(object)property.ValueString; // 可按需扩展更多支持的类型(如int、CodeableConcept等) default: throw new NotSupportedException($"Type {typeof(T)} is not supported for Property value extraction."); } } }
使用示例
调用扩展方法就能像GetExtensionValue一样便捷获取值:
var targetConcept = result.Concept.First(c => c.Code == "BOOKED_CLINICAL_NEED"); var effectiveFrom = targetConcept.Property.GetPropertyValue<DateTime>("effectiveFrom"); var commentIsMandatory = targetConcept.Property.GetPropertyValue<bool>("commentIsMandatory"); var canCancelAppointment = targetConcept.Property.GetPropertyValue<bool>("canCancelAppointment");
手动处理的替代方案(不推荐)
如果不想封装方法,也可以直接手动查找并转换,但代码冗余度高:
var commentProp = targetConcept.Property.First(p => p.Code == "commentIsMandatory"); bool commentIsMandatory = commentProp.ValueBoolean.HasValue ? commentProp.ValueBoolean.Value : false;
封装扩展方法的核心优势是复用性强,后续其他场景处理Property时直接调用即可,不用重复编写查找和类型转换逻辑。
内容的提问来源于stack exchange,提问作者Steve Borman
相关产品推荐
相关产品推荐

