I have problem with the following lambda expression:
public IEnumerable<ModuleEntity> GetPageList(Pagination pagi, string query)
{
var expression = LinqExpression.Default<ModuleEntity>();
expression = expression.And(e =>
e.DeleteMark != 1 &&
e.EnableMark != 0
);
return this.BaseRepository().GetList(expression, pagi);
}
This is my code, I want to add default expression to my business.
Note: the properties EnableMark
and DeleteMark
are nullable<int>
.
When I debug, the lambda expression translate the expression to this:
{p => ((1 == 1) AndAlso ((p.DeleteMark != Convert(1)) AndAlso (p.EnableMark != Convert(0))))};
It leads to my Query throwing an exception:
// use Dapper connection Query
var query = dbConnection.Query<T>(linq);
Exception: Unrecognized NodeType (Convert),
I can't figure out why the lambda expression gives me Covnert(1)
and Convert(0)
, any suggestions?
I tried to change the property EnableMark
and DeleteMark
to int
instead of nullable<int>
, then it worked. But in the other cases, nullable type
is necessary, is there any solutions or documents ?
Thanks.
@Eric Lippert Thank you for pointing out the causes, I set a variable of value '1', let it's type to int?
, and assigned to DeleteMark. It worked !!
public IEnumerable<ModuleEntity> GetPageList(Pagination pagi, string query)
{
var expression = LinqExpression.Default<ModuleEntity>();
int? deleteMark = 1;
int? enableMark = 0
expression = expression.And(e =>
e.DeleteMark != deleteMark &&
e.EnableMark != enableMark
);
return this.BaseRepository().GetList(expression, pagi);
}
I can't figure out why the lambda expression gives me Convert(1) and Convert(0)
As you note, the DeleteMark and EnableMark are nullable ints. C# does not define a comparison operator between ints and nullable ints. It does define a comparison operator between two nullable ints, and a conversion from int to nullable int. The lambda conversion captures the fact that the integers 1 and 0 have been converted to nullable integers in order to do the comparison.