× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Select specific element by using Linq

Asad Saeeduddin
Oct 08, 2015
<p>You could write it a bit more succintly:</p> <pre><code>var t = texts.FirstOrDefault(x =&gt; x.Display == Display.Terminal) ?? texts.FirstOrDefault(); if ( t != null ) content = t.Value; </code></pre> <p>If you're using the latest C# you can even use the null propagation operator to make it just a single expression:</p> <pre><code>var content = (texts.FirstOrDefault(x =&gt; x.Display == Display.Terminal) ?? texts.FirstOrDefault())?.Value; </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31207965/Select%20specific%20element%20by%20using%20Linq/31208056">Stack Overflow</a>.</p>
comments powered by Disqus