FilterExpression issue - ToList vs Not ToList

2 posts, 1 answers
  1. Barbaros Saglamtimur
    Barbaros Saglamtimur avatar
    110 posts
    Member since:
    Jul 2012

    Posted 10 Oct 2011 Link to this post

    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"),
        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.

  2. Answer
    Veli avatar
    2002 posts

    Posted 13 Oct 2011 Link to this post

    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.

    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
Back to Top