firstordefault will occupy several lines, but just return one of them first, if it is null it can handle an exception At first it will take several lines, but just return the first of them, if it is null this will throw an exception singleordefault will occupy only one line, but will be returned , it can handle exceptions, if it is zero single it will take only one line, but will return it and will not be able to handle exceptions
If your result set returns 0 records:
SingleOrDefault returns the default value for the type (for example, the default value for int is 0) FirstOrDefault returns the default value for the type. As a result, the result returns 1 record:
SingleOrDefault returns this record FirstOrDefault returns this record If your result set returns many records:
SingleOrDefault throws an exception. FirstOrDefault returns the first record. Output:
If you want to throw an exception if the result set contains many records, use SingleOrDefault.
If you always want 1 record, no matter what the result set contains, use FirstOrDefault
user3364059
source share