LINQ Question

Nate Bross

Well-known member
Joined
Apr 6, 2005
Messages
601
Location
Chicago, IL
I have this code which works great

Code:
            var newList = new Object();
            newList = from Task t in FoundTasks
                      where t.TaskStatus.Contains(sStatusFilter) &&
                      t.TaskType.Contains(sTaskFilter)
                      select t;
            // bind the result to the data grid
            dgvMyTasks.DataSource = newList;
            dgvMyTasks.DataBind();

The issue is that if I change the properties of dgvMyTasks to allow paging, I get the error "this object does not support server-side data paging"

I have created this workaround, but Im wondering if there is a better way:

Code:
            List<Task> newList = new List<Task>();

            foreach (Task t in (from Task t in FoundTasks
                               where t.TaskStatus.Contains(sStatusFilter) &&
                               t.TaskType.Contains(sTaskFilter)
                               select t))
            {
                newList.Add(t);
            }

            // bind the result to the data grid
            dgvMyTasks.DataSource = newList;
            dgvMyTasks.DataBind();

I got the workaround above with the loop since this, more direct approach (type cast):

Code:
            List<Task> newList = new List<Task>();
            newList = (List<Task>)from Task t in FoundTasks
                      where t.TaskStatus.Contains(sStatusFilter) &&
                      t.TaskType.Contains(sTaskFilter)
                      select t;

            // bind the result to the data grid
            dgvMyTasks.DataSource = newList;
            dgvMyTasks.DataBind();

throws the exception "unable to cast from type WhereIterator to Task.

The Linq query is the same in all three code snippets.

Is there a better solution out there?
 
As a quick point I would change your first sample to
C#:
var newList = from Task t in FoundTasks
    where t.TaskStatus.Contains(sStatusFilter) &&
    t.TaskType.Contains(sTaskFilter)
    select t;
that way var should be based on the result of the Linq expression rather than object.

Have you tried using a LinqDataSource and binding the grid to that rather than assigning the result directly to the grid?

Failing that you might want to handle the paging events yourself and take advantake of Linqs .Skip and .Take methods to do the actual paging.

Out of interest are you using Linq to Sql here or linq against some other object model / collection?
 
Thanks for the response; however, changing to your sugested definition the object newList remains of type:
"{System.Linq.Enumerable.WhereIterator<Task>}"
and results in the exception:
"The data source does not support server-side data paging."

I am using a List<Task> object as my datasource, where Task is a class Ive created so this is not linq to sql.

I was trying to impliment my own paging in the first place.
Failing that you might want to handle the paging events yourself and take advantake of Linqs .Skip and .Take methods to do the actual paging.
Could you provide some additional resources on this? how would I go about using this?
 
As a quick example
C#:
int[] x = {1, 2, 2, 34, 234, 123, 3, 234, 12, 123, 45, 4, 14, 132, 123, 123, 23};

var res = x.Skip(4).Take(3);
should result in res containing 234, 123, 3

you could then use something similar to
C#:
var newList = (from Task t in FoundTasks
    where t.TaskStatus.Contains(sStatusFilter) &&
    t.TaskType.Contains(sTaskFilter)
    select t).Skip(10).Take(10);
to restrict the results to a certain range.
 
Resolved the issue by using LINQs ToList method. I feel stupid for not finding this method sooner...

For anyone else who has this issue:
C#:
// define variables
List<Task> newList;
            
// make "select"
newList = (from Task t in FoundTasks
       where t.TaskStatus.Contains(sStatusFilter) &&
       t.TaskType.Contains(sTaskFilter)
       select t).ToList();

// bind the result to the data grid
dgvMyTasks.DataSource = newList;
dgvMyTasks.DataBind();
 

Similar threads

Back
Top