SQL Linq, query and then take result to query again
-
So I thought I was getting this down now. In my original code, I used 1 function to get the name, and another function to get the record results. So I rewrote it Linq, But I'm not quite getting how it works. So the first query called contributor returns a result that looks like SQL sort of, and says to expand to see the results. The 2nd query does the same thing, and the pValue for the record count is non zero, and triggers an exception of non zero. So I'm missing something on the concept here, and can't figure it out. I shortened the sample for easier reading.
Public Shared Function load_movieContributor_array( _
ByVal pMovieID As Integer,
ByVal pPageIndex As Integer,
ByVal pPageSize As Integer,
ByRef pResults As IEnumerable(Of MovieIndex)) As IntegerDim pValue As Integer = 0 Dim context As New MoviesContext() Dim contributor = \_ (From m In context.Movies Where m.MovieID = pMovieID Select m.MovieContributor) Dim movies As IQueryable(Of Movies) = context.Movies movies = movies.Where(Function(m) m.MovieContributor = contributor) movies = movies.OrderBy(Function(m) m.MovieName) 'Get a Single Page of Data If (pPageIndex = 1) Then movies = movies.Take(pPageSize) Else movies = movies.Skip((pPageIndex - 1) \* pPageSize).Take(pPageSize) End If pResults = \_ movies.AsEnumerable().Select(Function(item) New MovieIndex With { .MovieID = item.MovieID, 'More columns ..... }) pValue = pResults.Count() Return pValue End Function
-
So I thought I was getting this down now. In my original code, I used 1 function to get the name, and another function to get the record results. So I rewrote it Linq, But I'm not quite getting how it works. So the first query called contributor returns a result that looks like SQL sort of, and says to expand to see the results. The 2nd query does the same thing, and the pValue for the record count is non zero, and triggers an exception of non zero. So I'm missing something on the concept here, and can't figure it out. I shortened the sample for easier reading.
Public Shared Function load_movieContributor_array( _
ByVal pMovieID As Integer,
ByVal pPageIndex As Integer,
ByVal pPageSize As Integer,
ByRef pResults As IEnumerable(Of MovieIndex)) As IntegerDim pValue As Integer = 0 Dim context As New MoviesContext() Dim contributor = \_ (From m In context.Movies Where m.MovieID = pMovieID Select m.MovieContributor) Dim movies As IQueryable(Of Movies) = context.Movies movies = movies.Where(Function(m) m.MovieContributor = contributor) movies = movies.OrderBy(Function(m) m.MovieName) 'Get a Single Page of Data If (pPageIndex = 1) Then movies = movies.Take(pPageSize) Else movies = movies.Skip((pPageIndex - 1) \* pPageSize).Take(pPageSize) End If pResults = \_ movies.AsEnumerable().Select(Function(item) New MovieIndex With { .MovieID = item.MovieID, 'More columns ..... }) pValue = pResults.Count() Return pValue End Function
Public Shared Function load_movieContributor_array( _
ByVal pMovieID As Integer,
ByVal pPageIndex As Integer,
ByVal pPageSize As Integer,
ByRef pResults As IEnumerable(Of MovieIndex)) As IntegerDim pValue As Integer = 0 Dim context As New MoviesContext() Dim cResults = \_ (From m In context.Movies Where m.MovieID = pMovieID Select m.MovieContributor).SingleOrDefault() Dim m\_contributor As String = cResults Dim movies As IQueryable(Of Movies) = context.Movies movies = movies.Where(Function(m) m.MovieContributor = m\_contributor) movies = movies.OrderBy(Function(m) m.MovieName) 'Get a Single Page of Data If (pPageIndex = 0) Then movies = movies.Take(pPageSize + 1) Else movies = movies.Skip((pPageIndex + 1) \* pPageSize).Take(pPageSize) End If