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 9322115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:00:45+00:00 2026-06-19T04:00:45+00:00

I have a For each loop that is using linq to get info from

  • 0

I have a For each loop that is using linq to get info from the Visit table. It is nested with another For each loop that gets a name to list in the first column:

Caregiver | week1 | week2|....
__________|_______|______|....
John Smith|   2   |   3  |....
Mary Jones|   0   |   1  |....

It goes for 9 weeks and picks up a “Visit” count in the nested loop. I believe when I leave the inner loop it gives me a NULL error when it reaches the last Caregiver in the outer loop, and has an issue starting again from the top row for the next week (Week2).

I am getting an InvalidOperationException on the Select statement before I query the Visit table based on my Linq “visits”. What can I do to fix this?

EDIT: Here is an image of the error: https://i.stack.imgur.com/7oVtd.png

var phs = from cg in Context.CareGivers
          join cgg in Context.CareGiverGroups on cg.car_gvr_int_id equals cgg.car_gvr_int_id
          join o in Context.Organizations on cgg.org_int_id equals o.org_int_id
          where cg.row_sta_cd.Trim() == "A"
          && cgg.alt_phy_id != null
          && cgg.alt_phy_id.Trim() != String.Empty
          && o.cli_acc_fg.Trim() == "Y"
          && o.org_int_id == 1468461
          select cg;

int r = 1;
int s = 1;

for (int i = 0; i < 9; i++)
   {
     start = start.AddDays(i * -7);
     end = start.AddDays(7);

     foreach (var cg in phs)
             {
             // grab the correct exception, this will allow us to figure out where the issue might be
              var visits = cg.CareGiverFunction.First(cgf => cgf.CodeDetail.cod_dtl_ds.Trim() == "Family Physician").VisitCareGiver.Select(vcg => vcg.Visit).AsQueryable();
              visits = visits.Where(v => v.adm_ts >= start && v.adm_ts < end
                       && (v.CodeDetail.cod_dtl_ext_id.Trim() == "I" || v.CodeDetail.cod_dtl_ext_id.Trim() == "V")
                       && v.VisitStatusCdCodeDetail.cod_dtl_ext_id.Trim() != "CANCEL");


               int counter = visits.Count();
               String phys = cg.Person.DisplayName();

               workbook.AddCell(r, 0, phys);
               workbook.AddCell(r, s, counter);
               r++;

              }
              s++;

    }
  • 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-19T04:00:47+00:00Added an answer on June 19, 2026 at 4:00 am

    cg.CareGiverFunction.First() might not be returning a result, this means that the rest of the code you have on that line doesn’t have any data to compare against.

    Jon Skeet has answered a similar question, I’ll direct you to his post as I doubt I’ll be able to answer it any better than him: see here

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

Sidebar

Related Questions

I have a For Each loop that is looping through an array of strings
I have a loop that enters each non-empty text field into a database: foreach
I have a loop that can look like this: For Each article In artAll
I have some code that runs a model in a loop. Each iteration of
The following code returns the error from the For Each loop. I have similar
I'm using VB 2010 Express. I have a loop that calls a function named
If I have a Publisher table that has many Books, and each Book can
I have the following function that returns results from the database based on LINQ
I have a controller that is building a query from Linq to Sql to
I am Using LINQ to query a table that has a foreign key for

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.