customerInfo.Telephone = contactData.Where(d => d.ContactTypeId == (int)ContactType.Phone).FirstOrDefault() != null
? contactData.Where(d => d.ContactTypeId == (int)ContactType.Phone).FirstOrDefault().Data
: string.Empty;
contactData
是IEnumerator。問題是運行相同的查詢兩次。如果我使用變量,我可以擺脫它,但是然後有一個新的變量需要維護。
有沒有辦法讓這段代碼更具可讀性並使其運行得更快而不使用任何其他自定義庫?如何改進這個C#Linq代碼?
一起使用了這個異常:'System.NotSupportedException:無法創建類型爲'Contact'的常量值。 ' –
這對我有效:'customerInfo.Telephone = contactData.Where(d => d.ContactTypeId ==(int)ContactType.Phone1).Select(d => d .Data) .DefaultIfEmpty(string.Empty) .First();' –