Dan
Dan

Reputation: 724

Comparison operators not supported for type IList when Paging data in Linq to Sql

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.

Upvotes: 0

Views: 435

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 457147

Try doing an explicit ordering before Skip and Take.

Upvotes: 0

Coding Flow
Coding Flow

Reputation: 21881

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

Upvotes: 1

Related Questions