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

FilterExpression issue - ToList vs Not ToList

1 Answer 68 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Barbaros Saglamtimur
Top achievements
Rank 1
Barbaros Saglamtimur asked on 10 Oct 2011, 08:47 AM
Can someone explain why FilterExpression is LINQ syntax for List but SQL syntax for others.

Here is what I have tested:
public class TheData
{
    public string Subject { get; set; }
}
 
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    MyEntity ent = new MyEntity();
    IQueryable<TheData> gridData = from t1 in ent.Documents
                                   join t2 in ent.Approve on t1.Id equals t2.DocumentId
                                   where (t1.InsertById == 1 || t1.ApprovedById == 1)
                                   select new TheData
                                              {
                                                  Subject = t1.Subject
                                              };
    string filterExpression = RadGrid1.MasterTableView.FilterExpression.Replace(".ToString()", String.Empty);
    RadGrid1.VirtualItemCount = filterExpression != string.Empty
                                    ? gridData.Where(filterExpression).Count()
                                    : gridData.Count();
 
    var testData = (filterExpression != string.Empty
                                               ? gridData.Where(filterExpression)
                                               : gridData).ToList();
 
    var finalData = (from f in testData
                               select new
                                          {
                                              Subject = f.Subject.Replace("someval", "tosomeval"),
                                          }).ToList();
    RadGrid1.DataSource = finalData;
}

If I call .ToList() for the finalData filterExpressions syntax is
(iif(Subject == null, "", Subject).Contains("sometext"))

but if I do not call .ToList() filterExpression syntax is
([Subject] LIKE '%sometext%')

PS: EnableLinqExpressions="true" doesnt reflect any changes.

TIA.

1 Answer, 1 is accepted

Sort by
0
Accepted
Veli
Telerik team
answered on 13 Oct 2011, 02:00 PM
The reason for RadGrid switching to SQL-style filter formats is because it cannot use LINQ for filtering your data, or at least cannot use all LINQ APIs required for successful filtering.

The thing is, the LINQ query you assign to RadGrid.DataSource provides a  forward-only enumerator. A forward-only enumerator is one that does not support Reset(). An enumerator that does not supports resets cannot be manipulated with LINQ in RadGrid. That is why, LINQ falls back to SQL syntax and performs filtering and sorting using a DataTable approach, not a LINQ query approach.

Conversely, when you call your LINQ query's ToList() method, you are getting your data in a two-way enumerator, meaning the enumerator can be rolled back and iterated as many times as required. Now RadGrid can use LINQ for data operations like filtering and your FilterExpression is in LINQ format.

Veli
the Telerik team
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 their blog feed now
Tags
Grid
Asked by
Barbaros Saglamtimur
Top achievements
Rank 1
Answers by
Veli
Telerik team
Share this question
or