FirstOrDefault returns NullReferenceException if no match is found

后端 未结 7 2149
广开言路
广开言路 2021-02-05 00:26

Here is my code:

string displayName = Dictionary.FirstOrDefault(x => x.Value.ID == long.Parse(options.ID)).Value.DisplayName;

The code works

7条回答
  •  不要未来只要你来
    2021-02-05 01:04

    To add to the solutions, here is a LINQ statement that might help

    Utilities.DIMENSION_MemTbl.Where(a => a.DIMENSION_ID == format.ContentBrief.DimensionID).Select(a=>a.DIMENSION1).DefaultIfEmpty("").FirstOrDefault();
    

    The result will be an empty string if the result of the query is a null..

提交回复
热议问题