Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:55:19+00:00 2026-06-02T00:55:19+00:00

I’m flummoxed. I’m using VB.Net, Linq, and a DataContext. My DataContext contains a table

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:55:21+00:00Added an answer on June 2, 2026 at 12:55 am

    I’m not to familiar with PredicateBuilder but I think you are capturing (closing over) the chosenCode variable there.

    That means your testRun3 is 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 then Next.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.