I’m having issues implementing the TOP or SKIP functionality when building a new object query.
I can’t use eSQL because i need to use an “IN” command – which could get quite complex if I loop over the IN and add them all as “OR” parameters.
Code is below :
Using dbcontext As New DB
Dim r As New ObjectQuery(Of recipient)("recipients", dbcontext)
r.Include("jobs")
r.Include("applications")
r = r.Where(Function(w) searchAppIds.Contains(w.job.application_id))
If Not statuses.Count = 0 Then
r = r.Where(Function(w) statuses.Contains(w.status))
End If
If Not dtFrom.DbSelectedDate Is Nothing Then
r = r.Where(Function(w) w.job.create_time >= dtDocFrom.DbSelectedDate)
End If
If Not dtTo.DbSelectedDate Is Nothing Then
r = r.Where(Function(w) w.job.create_time <= dtDocTo.DbSelectedDate)
End If
'a lot more IF conditions to add in additional predicates
grdResults.DataSource = r
grdResults.DataBind()
If I use any form of .Top or .Skip it throws an error : Query builder methods are not supported for LINQ to Entities queries
Is there any way to specify TOP or Limit using this method? I’d like to avoid a query returning 1000’s of records if possible. (it’s for a user search screen)
Rather than
r = new ObjectQuery<recipient>("recipients", dbContext)try
r = dbContext.recipients..Skip()and.Take()returnIOrderedQueriable<T>while.WherereturnsIQueriable<T>. Thus put the.Skip()and.Take()last.Also change
grdResults.DataSource = rtogrdResults.DataSource = r.ToList()to execute the query now. That’ll also allow you to temporarily wrap this line in try/catch, which may expose a better message about why it’s erroring.