I’m flummoxed.
I’m using VB.Net, Linq, and a DataContext. My DataContext contains a table ‘transactions’.
I first declare an IQueryable(Of transaction) and assign it to nothing. I build a predicate in a foreach loop and use a transactions.Where(predicate) to assign the IQueryable a value. If I do an IQueryable.ToList(), I get a number of items in the collection.
However, on the next iteration of the loop, the IQueryable.ToList() gives me 0 items.
This is driving me crazy. I tried to use the LINQ to SQL Debug Visualizer with VS2010, (I recompiled the 2008 version with the new reference) but no dice – I can’t see the SQl generated or what’s inside the IQueryable.
Here’s the code:
Dim groupQuery As IQueryable(Of transaction) = Nothing
For Each chosenCode As BillingCode In chosenGroup.BillingCodes
Dim testRun As List(Of transaction) = Nothing
If Not groupQuery Is Nothing Then
testRun = groupQuery.ToList()
End If
Dim codePredicate = PredicateBuilder.True(Of transaction)()
codePredicate = codePredicate.And(Function(i) i.code.Equals(chosenCode.Code))
If Not chosenCode.Description Is Nothing Then codePredicate = codePredicate.And(Function(i) i.description.ToUpper().Contains(chosenCode.Description.ToUpper()))
If Not chosenCode.Insurance Is Nothing Then codePredicate = codePredicate.And(Function(i) i.v_patient.insname.ToUpper().Contains(chosenCode.Insurance.ToUpper()))
If Not chosenCode.PriceFloor Is Nothing Then codePredicate = codePredicate.And(Function(i) i.charge >= chosenCode.PriceFloor)
If Not chosenCode.PriceCeiling Is Nothing Then codePredicate = codePredicate.And(Function(i) i.charge <= chosenCode.PriceCeiling)
Dim testRun2 As List(Of transaction) = Nothing
Dim testRun3 As List(Of transaction) = Nothing
If groupQuery Is Nothing Then
groupQuery = vf.transactions.Where(codePredicate)
Else
testRun2 = groupQuery.ToList()
groupQuery = groupQuery.Union(vf.transactions.Where(codePredicate))
testRun3 = groupQuery.ToList()
End If
Next
I’d appreciate any help. Thanks.
EDIT: The ‘testRun’ variables are only there for me to debug the code. What I’m trying to do is groupQuery = groupQuery.Union(QUERY FROM THIS ITERATION) without executing the query until I transform it a couple more times in the code. Sorry for the confusion.
ANSWER EDIT: Both of the answers I received contributed to the final solution. I ended up both calling ToList() on each iteration, and using Concat() instead of Union(). I figured out that each time I ran Union() that the generated SQL would contain parameters (@variable) for that iteration of chosenCode. by the time I ran the query, I might have 10 or twenty sets of parameters in the generated SQL (one set for each iteration of chosenCode) but only the final set of parameters were being supplied to SQL Server when it ran. Don’t ask me why. This is not even proven, just a conclusion through experimentation. Once I figured this out, I couldn’t figure out how to get all of the parameters to be passed. So, I gave up and run the query to SQL Server every iteration of chosenCode. I’d still like to know how to do it without running the query until it’s fully built.
Instead of working on an IQueryable, I use a strongly typed List(Of transaction) to store the results of ToList(). I then manipulate that list using Linq to Objects throughout the rest of my code.
I also found that using Union() produces a UNION statement in the generated SQL, but that wasn’t giving me the results I expected. So, I used Concat(), which produces a UNION ALL statement in the generated SQL, which gives me exactly what I’m after.
Thanks, for everybody’s help. Here’s the code:
For Each chosenGroup As BillingGroup In chosenGroups
Dim groupResults As List(Of transaction) = Nothing
For Each chosenCode As BillingCode In chosenGroup.BillingCodes
Dim codePredicate = PredicateBuilder.True(Of transaction)()
codePredicate = codePredicate.And(Function(i) i.code.Equals(chosenCode.Code))
If Not chosenCode.Description Is Nothing Then codePredicate = codePredicate.And(Function(i) i.description.ToUpper().Contains(chosenCode.Description.ToUpper()))
If Not chosenCode.Insurance Is Nothing Then codePredicate = codePredicate.And(Function(i) i.v_patient.insname.ToUpper().Contains(chosenCode.Insurance.ToUpper()))
If Not chosenCode.PriceFloor Is Nothing Then codePredicate = codePredicate.And(Function(i) i.charge >= chosenCode.PriceFloor)
If Not chosenCode.PriceCeiling Is Nothing Then codePredicate = codePredicate.And(Function(i) i.charge <= chosenCode.PriceCeiling)
If groupResults Is Nothing Then
groupResults = vf.transactions.Where(codePredicate).ToList()
Else
groupResults.AddRange(vf.transactions.Where(codePredicate).ToList())
End If
Next
I’m not to familiar with PredicateBuilder but I think you are capturing (closing over) the
chosenCodevariable there.That means your
testRun3is filled with the chosenCode of that run, but in the next iteration the testRun is filled with the new chosenCode.The solution is simple, do a
groupQuery = groupQuery.ToList()just before thenNext.