Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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?

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • 2 weeks later...
Posted

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();

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...