I'm working on custom filter with System.Linq.Dynamic
, everything was ok until i start the fight with BETWEEN
.
I created a custom class called Filtro
, it has this propertys .
public string PropertyName { get; set; }
public string Value1 { get; set; }
public string Value2 { get; set; }
Then, i basically filter a IQueryable
object with the parameters of that class
I'm trying this:
var query = db.Where("@0 > @1 AND @0 < @2", filtro.PropertyName, filtro.Value1, filtro.Value2);
My problem is that the parameters are generated incorrectly in the query.
Example:
filtro.PropertyName = "example";
filtro.Value1 = "10";
filtro.Value2 = "20";
When i generate query without parameters
var query = db.Where("example>10 && example<20");
It generates this:
SELECT
[Extent1].[randomField] AS [randomField],
[Extent1].[example] AS [example],
FROM [dbo].[Transformador] AS [Extent1]
WHERE ([Extent1].[example] > cast(10 as decimal(18))) AND ([Extent1].[example] < cast(20 as decimal(18)))
ORDER BY [Extent1].[randomField] ASC}
But when i use parameters (as i show before) i'm getting this
SELECT
[Extent1].[randomField] AS [randomField],
[Extent1].[example] AS [example]
FROM [dbo].[Transformador] AS [Extent1]
WHERE (N'example' > N'10') AND (N'example' < N'20')
ORDER BY [Extent1].[randomField] ASC
I guess I'm using the parameters of Linq Dynamics wrong, but I did not find anything similar going around on the internet
I hope someone know what the problem is, thanks in advance!