一、問題描述
在做C# 的 Guid 轉換時,出現這個問題:Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). 中文翻譯是:guid應包含32位數字和4個破折號(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX)。
二、解決方案
看我們轉換的字符串是否滿足這個條件:guid應包含32位數字和4個破折號(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX),不滿足修改滿足即可!
三、案例
我在轉換的時候,由於我需要轉換的字符串,不滿足guid的規則,才出現這個錯誤。
仔細觀察,我需要轉換的字符串,根本就不滿足 guid 的特點,當我把 ar 中的 “ " ” 號去掉就可以了。
OK,轉換完成!
---------------修改2019年05月09日---------------------
因為我的 DataId 本來就是一個Json 字符串,所以沒有必要去使用 Replace() 、Split(),直接使用 JsonConvert 操作多好,所以我改為:
var items = JsonConvert.DeserializeObject<List<Guid>>(_recordBookRepository.Get(input.RecordBookId).DataId);
讓專業的方法,處理專業的事情,是否是最優解。
---------------修改2019年05月23日---------------------
判斷 Guid 是否為空。
傳入的參數:
/// <summary> /// id /// </summary> public Guid? StatGroupId { get; set; }
判斷是否為空:
var boolStatGroup = input.StatGroupId.HasValue && input.StatGroupId != Guid.Empty;