Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq expression throws an ArgumentNullException

Tags:

c#

linq

GetSpecialNodes returns null sometimes. When it does, I get an ArgumentNullException thrown. Is there an elegant way of handling (a change to the linq expression) this besides calling GetSpecialNodes before running the Linq expression and doing a null check?

var nodes = (from HtmlNode node in document.GetSpecialNodes() select node);
like image 867
MedicineMan Avatar asked Feb 22 '26 15:02

MedicineMan


1 Answers

May be

var nodes = (document.GetSpecialNodes() ?? new List<HtmlNode>()).ToList<HtmlNode>()
like image 113
Francois Avatar answered Feb 25 '26 05:02

Francois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!