質問

I'm writing a function to return a collection of data. Let's assume we have

Dim myQuery = (From B In ctx.MyTable.AsQueryable
               Select
                   SomeValue = CType(B.SomeValue, Decimal?),
                   AnotherValue = CType(B.AnotherValue, Decimal?))

myQuery = myQuery.Where(Function(x) x.SomeValue<50)
Dim myInterimResult = myQuery.ToList

How do I query MyInternalResult on the server filtering only through the results of original query?

What i'm aiming at is a double filter. as in let's say I want to first filter Some value so that only values <50 are returned... and the a secondary filter that filters the result of the previous further...

WIll I have to do .ToList on my first iqueryable filter and then only filter the results local in .net collection? My concern is that even though not at the moment my results can get quite large 10000+ records. i'd like to keep my workload to be done on sql server rather than in memory. the code I have is quite large so I just made a simple example to illustrate my point.

役に立ちましたか?

解決

No, you shouldn't call ToList between applying different filtering conditions. Just call Where again:

Dim myQuery = (From B In ctx.MyTable.AsQueryable
               Select
                   SomeValue = CType(B.SomeValue, Decimal?),
                   AnotherValue = CType(B.AnotherValue, Decimal?))

myQuery = myQuery.Where(Function(x) x.SomeValue < 50)

myQuery = myQuery.Where(Function(x) x.AnotherValue < 20)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top