4

I am using dynamic LINQ (System.Linq.Dynamic)(you may find description here, http://dynamiclinq.azurewebsites.net/GettingStarted).

The following statement works well

Products.Select("new(ProductName, CategoryID.CategoryName as CategoryName)");

But I accidentally found when CategoryID is null, the results are empty. But I supposed it would return a record such as:

ProductName="Wine", CategoryName="" (or null).

Then I found a way to do so by

Products.Select("new(ProductName, iif(CategoryID==null,\"\",CategoryID.CategoryName) as CategoryName)");

The statement is ugly.

Do you have a better solution?

Thank you in advance,

Ying
  • 526
  • 4
  • 16
  • 1
    "new(ProductName, CategoryID==null?null:CategoryID.CategoryName) as CategoryName)" worked. It seems a little simpler and more general to use null, as Cory mentioned. – Ying May 20 '15 at 02:06
  • A more ugly sample is : grand==null?null:(grand.parent==null?null:grand.parent.son). – Ying May 20 '15 at 02:50

1 Answers1

4

The only thing I found is here. It isn't clear why the solution was accepted, but what I did see there is that you can do this:

"new(ProductName, iif(CategoryID==null,null,CategoryID.CategoryName) as CategoryName)"

instead of this:

"new(ProductName, iif(CategoryID==null,\"\",CategoryID.CategoryName) as CategoryName)"

It isn't any shorter, but to me it makes the code a bit more readable because you just use null instead of escaping the quotes.

Community
  • 1
  • 1
Cory
  • 783
  • 5
  • 12