Nate Bross Posted May 27, 2008 Posted May 27, 2008 I have this code which works great 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 I'm wondering if there is a better way: 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): 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? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted May 27, 2008 Administrators Posted May 27, 2008 As a quick point I would change your first sample to 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 Linq's .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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted May 27, 2008 Author Posted May 27, 2008 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 I've 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 Linq's .Skip and .Take methods to do the actual paging. Could you provide some additional resources on this? how would I go about using this? Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted May 27, 2008 Administrators Posted May 27, 2008 As a quick example 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 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Nate Bross Posted June 10, 2008 Author Posted June 10, 2008 Resolved the issue by using LINQ's ToList method. I feel stupid for not finding this method sooner... For anyone else who has this issue: // 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(); Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.