views:

57

answers:

2

I can understand what the error is saying - it can't compare a list. Not a problem, other than the fact that I don't know how to not have it compare a list when I want to page through a model with a list as a property.

My Model:

Event : IEvent
  int Id
  string Title
  // Other stuff..
  LazyList<EventDate> Dates // The "problem" property

Method which pages the data (truncated):

public JsonResult JsonEventList(int skip, int take) {

  var query = _eventService.GetEvents();

  // Do some filtering, then page data..

  query = query.Skip(skip).Take(take);

  return Json(query.ToArray());
}

As I said, the above is truncated quite a bit, leaving only the main point of the function: filter, page and return JSON'd data.

The exception occurs when I enumerate (.ToArray()). The Event object is really only used to list the common objects of all the event types (Meetings, Birthdays, etc - for example). It still implements IEvent like the other types, so I can't just remove the LazyList<EventDate> Dates property unless I no longer implement IEvent

Anyway, is there a way to avoid this? I don't really want to compare a LazyList when I page, but I do not know how to resolve this issue.

Thank you in advance! :)

+1  A: 

Do a transform on the data before returning it as JSON if you don't want the list e.g.

return Json(query.Select(event => new { event.Id, event.Title }).ToArray());
Ben Robinson
Will `query.Select(...)` enumerate the query before I Skip and Take, though? Worth a try, anyway.
Dan
ToArray() will enumerate query. It does not really matter what order you do the .Skip .Take .Select becuase until you call ToArray() it is just building up an expression tree.
Ben Robinson
Just tried truncating the data then .skip'ing but I get an error saying Skip has to be over ordered queries containing all columns of a table. This may be because I'm using SQL Server 2000 :(
Dan
What kind of filtering are you doing, perhaps that's where the problem lies?If you put the Skip() Take() after the ToArray() it would probably work, not good though, it would mean that it would retreive the whole dataset and then discard what the rows you don't want.
Ben Robinson
I have Skip/Take working on another table just fine - I think I'll just do some research and open up another question if need be :)
Dan
@Ben - here's the problem: I have a repository method that returns Event objects - so when I try to transform the selection then .skip, it fails because I'm trying to do skip over entities :-/. I'll figure it out - just wanted to update you.
Dan
A: 

Try doing an explicit ordering before Skip and Take.

Stephen Cleary
Tried `query.OrderBy("col", "asc").Skip(...).Take(...)` with same error
Dan