This is a migrated thread and some comments may be shown as answers.

FilterExpression Linq

4 Answers 389 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Rhodes
Top achievements
Rank 1
David Rhodes asked on 08 Nov 2013, 09:07 AM
I'm trying to get the filterExpressions working on my Linq query using the Dynamic Linq Library but am getting this error

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

It looks like the filterExpression uses ToString() which is causing the error

(iif(LogType.Name == null, "", LogType.Name).ToString().Contains("Error"))

My code is

string filterExpression = (sender as RadGrid).MasterTableView.FilterExpression;
 
logs = db.Logs
        .Where(x => x.SystemId.Equals(this.SystemId.Value))
        .Where(filterExpression)
        .OrderByDescending(x => x.OccurredOn)
        .Skip(skip)
        .Take(pageSize)
        .ToList();


4 Answers, 1 is accepted

Sort by
0
David Rhodes
Top achievements
Rank 1
answered on 08 Nov 2013, 09:17 AM
This seems to do the trick

string filterExpression = (sender as RadGrid).MasterTableView.FilterExpression.Replace(".ToString()", "");
0
David Rhodes
Top achievements
Rank 1
answered on 08 Nov 2013, 10:18 AM
It also errors with Date filters

LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method, and this method cannot be translated into a store expression.

Any idea how this can be fixed?
0
Antonio Stoilkov
Telerik team
answered on 13 Nov 2013, 08:27 AM
Hi David,

The experienced behavior is expected. Microsoft LINQ to Entities does not implement the DateTime.Parse method and this is a know issue. There are multiple topics on the Internet which state the same problem. You could take a look at discussion below which explains the problem:

Regards,
Antonio Stoilkov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
kuntal
Top achievements
Rank 1
answered on 11 Mar 2015, 09:37 AM
01.string FinalQuery = "";
02.            if (filter.Contains("DateTime.Parse("))
03.            {
04.                var filters = filter.Split(new string[] { "DateTime.Parse(" }, StringSplitOptions.RemoveEmptyEntries);
05.                for (int i = 0; i < filters.Count(); i++)
06.                {
07.                    if (i != 0)
08.                    {
09.                        var oldDate = filters[i].Substring(1, 22);
10.                        var newDate = DateTime.Parse(oldDate);
11.                        filters[i] = filters[i].Replace('\"' + oldDate + "\")", "DateTime(" + newDate.Year + ',' + newDate.Month + ',' + newDate.Day + ',' + newDate.Hour + ',' + newDate.Minute + ',' + newDate.Second + ")");
12.                    }
13.                    FinalQuery = FinalQuery + filters[i];
14.                }
15.            }
16.            else
17.                FinalQuery = filter;

I know this is quite an old question but this is how to get rid of that part of issue.
At least this is how I fixed it. 
Tags
Grid
Asked by
David Rhodes
Top achievements
Rank 1
Answers by
David Rhodes
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
kuntal
Top achievements
Rank 1
Share this question
or