簡體   English   中英

字符串不為空時的條件映射(ASP.NET Core)

[英]Conditional mapping if string not null (ASP.NET Core)

我在我的項目中使用 Automapper

在映射器中,我將字符串映射到ICollection

這是我如何做到的

.ForMember(x => x.PropertyImages,
                opt => opt.MapFrom(aa => aa.Attachments.Split(';', StringSplitOptions.None).ToList()));

但是如果字符串為空。 我有錯誤

對象未設置為對象的實例

我如何進行條件映射,僅當字符串不為空時

您可以使用三元運算符來檢查字符串


.ForMember(x => x.PropertyImages,
      opt => opt.MapFrom(aa => !string.IsNullOrEmpty(aa.Attachments) ? aa.Attachments.Split(';', StringSplitOptions.None).ToList() : new List<string>()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM