-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullable properties NullReferenceException
not thrown.
#181
Comments
Object reference not set to an instance of an object
not thrown.NullReferenceException
not thrown.
If you are building predicate for EF you are doing this in wrong way. |
@sdanyliv I am indeed doing that, I followed the demo you had in the Readme calling |
@elf-is You are referring to DataTableService.SearchAColumn? I believe, that you misunderstand the behaviour of EF Core and your Database here, and there isn't a lot LINQKit can do about this. It may also be, that you would need to push for a C# Language specification change. So first, what does EF Core do with
Second, if you intend to use the predicate outside EF Core you basically hit the limits of the C# compiler/C# Language specification. For all I know you can only use the expressions present in .NET 3.5 directly in source code and have it translated for you into an public class CoalescingTests
{
[Theory]
[InlineData(null, false)]
[InlineData("x", true)]
[InlineData("y", false)]
public void NullCoalesce(string value, bool expectation)
{
Expression<Func<string, bool>> expression = s => s.IfNotNull(str => str.Contains("x"), () => false);
Assert.Equal(expectation, expression.Invoke(value));
var expanded = expression.Expand();
Assert.DoesNotContain("IfNotNull", expanded.ToString());
Assert.Equal(expectation, expanded.Invoke(value));
}
}
public static class CoalescingExtensions
{
[Expandable(nameof(IfNotNullWithDefault))]
public static TResult IfNotNull<TIn, TResult>(this TIn value, Expression<Func<TIn, TResult>> then, Expression<Func<TResult>> @else)
{
return value != null ? then.Invoke(value) : @else.Invoke();
}
private static Expression<Func<TIn, Expression<Func<TIn, TResult>>, Expression<Func<TResult>>, TResult>> IfNotNullWithDefault<TIn, TResult>()
{
return (value, then, @else) => value != null ? then.Invoke(value) : @else.Invoke();
}
} Calling This would be an expansion with local variable (not supported for .NET 3.5): private static Expression<Func<TIn, Expression<Func<TIn, TResult>>, Expression<Func<TResult>>, TResult>> IfNotNullWithDefault<TIn, TResult>()
{
var valueParameter = Expression.Parameter(typeof(TIn), "value");
var value = Expression.Parameter(typeof(TIn), "v");
var then = Expression.Parameter(typeof(Expression<Func<TIn, TResult>>), "then");
var @else = Expression.Parameter(typeof(Expression<Func<TResult>>), "else");
return Expression.Lambda<Func<TIn, Expression<Func<TIn, TResult>>, Expression<Func<TResult>>, TResult>>(
Expression.Block(
new[] {value},
Expression.Assign(value, valueParameter),
Expression.Condition(
Expression.NotEqual(value, Expression.Constant(null, typeof(TIn))),
Expression.Invoke(then, value),
Expression.Invoke(@else))),
valueParameter, then, @else
);
} Update: tried to provide shorter code-samples. Defining & assigning variables sadly isn't supported in expression-trees, so it takes up more lines |
I encountered a small 'bug' on C# while trying to build a search method using the
stringVar.Contains
in a predicate.At first It worked fine for every Model I used it on, but if that Model had a
null
value in some of its properties no error is thrown but the search failed and returned no data.It took me a while to find where the problem was, and that's where I tried checking the values in my Model and found that some were
null
, usually we'd use the null-conditional operatorstringVar?.Contains
to check if the variable is null before using a method on it but we can't do that with expression trees (You'd get the following errorAn expression tree lambda cannot contain conditional access expressions
).So my fix was to check if the property is not null and then call the
string
method.And then called it in the predicate:
But I was wondering why the error was not thrown and if it was caught why does the predicate fail?
P.S: Might be the same problem here #62
The text was updated successfully, but these errors were encountered: