Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, Data Structure In computer science, a data structure is a particular way of organizing and storing data in a computer so that it can be accessed and modified efficiently. Learn More,

What is Cleanest way to check for Null value in Lambda Expressions, C#?

Description:Currently i am using following code and its not working,where Items is a list and GetHierarchicalTaxons("Category") is also returning a list which should have at least a item to pass the condition.

var Items=Model.Items.Where(i => i.GetHierarchicalTaxons("Category") != null);

Posted by: | Posted on: Feb 13, 2018

1 answers



You should have used the following code to check for null and check for at least an item exists,In your code you have missed a trick consider if GetHierarchicalTaxons("Category") returning a list with zero items that will also pass the condition in your code.
var Items=Model.Items.Where(i => i.GetHierarchicalTaxons("Category") != null && i.GetHierarchicalTaxons("Category").Any())

Replied by: | Replied on: Feb 28, 2018


Code block Hyperlink bold Quotes block Upload Images