Вопрос

I'm creating ASP.NET MVC4 C# application which accepts dynamic queries and generates razor views dynamically.

Application uses WebMatrix.Data.Query() to get data. This method returns IEnumerable<dynamic> . Result rows can contain bool? type properties whise value is null.

Those values are used in dynamically generated razor views with && , || and ! operators. Those operatos does not accept null boolean values, runtime exception occurs in this case.

I'm looking for a way to fix this. It seems that best way is to convert dynamic boolean null values to false.

I tried method

    public static dynamic BooleanNullToFalse(dynamic value)
    {
        if (value == null && value is bool?)
            return false;
        return value;
    }

but got strange compile error

One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?

in expression value == null

How to fix this so that code compiles ?

Another possibility would be to force &&, || and ! boolean operators to accept null values or define new boolean operators which accept null values. I dont know is this possible/reasonable?

Это было полезно?

Решение

Add the Microsoft.CSharp reference to your project, and if it is already present then remove and re-add it as per this comment.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top