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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:54:05+00:00 2026-06-10T15:54:05+00:00

StackOverflow! I have a MS SQL database. The part of this database is presented

  • 0

StackOverflow!

I have a MS SQL database. The part of this database is presented at the next picture

https://i.stack.imgur.com/DFnc5.png

I’m trying to make a complosable query, where I’m trying to find patients who had events with particular evEventKindID. For example, I want to find patients who have an event with (evEventKindtID == 1) and an event with (evEventKindID == 1).

var query = from pt in db.tblPatient
            select pt;
var list = query.ToList();// {1}

foreach (var limit in group.limits.Values)
{
      if (limit.eventKind.Type == TypeOfEventKind.ekEvent)
      {
           query = from pt in query
           where (pt.tblEvent.Count(j => j.evEventKindID == limit.eventKind.ID) > 0)
                                select pt;// {2}

           list = query.ToList();
           MessageBox.Show(query.Count().ToString());
      }
 }

The problem is that every next iteration can return more elements than the previous. It counfuses me. How can query from query return more entities than the first query?

In SQL Server Profiler I’ve found the SQL queries, generated by ADO .NET EF. In the place {1}:

SELECT 
[Extent1].[ptID] AS [ptID], 
[Extent1].[ptFullName] AS [ptFullName], 
[Extent1].[ptHomeAddress] AS [ptHomeAddress], 
[Extent1].[ptPhone] AS [ptPhone], 
[Extent1].[ptBirthDate] AS [ptBirthDate], 
[Extent1].[ptIsMale] AS [ptIsMale], 
[Extent1].[ptUserID] AS [ptUserID], 
[Extent1].[ptINN] AS [ptINN], 
[Extent1].[ptSNILS] AS [ptSNILS]
FROM [dbo].[tblPatient] AS [Extent1]

In the place {2} on the first iteration:

exec sp_executesql N'SELECT 
[Project1].[ptID] AS [ptID], 
[Project1].[ptFullName] AS [ptFullName], 
[Project1].[ptHomeAddress] AS [ptHomeAddress], 
[Project1].[ptPhone] AS [ptPhone], 
[Project1].[ptBirthDate] AS [ptBirthDate], 
[Project1].[ptIsMale] AS [ptIsMale], 
[Project1].[ptUserID] AS [ptUserID], 
[Project1].[ptINN] AS [ptINN], 
[Project1].[ptSNILS] AS [ptSNILS]
FROM ( SELECT 
    [Extent1].[ptID] AS [ptID], 
    [Extent1].[ptFullName] AS [ptFullName], 
    [Extent1].[ptHomeAddress] AS [ptHomeAddress], 
    [Extent1].[ptPhone] AS [ptPhone], 
    [Extent1].[ptBirthDate] AS [ptBirthDate], 
    [Extent1].[ptIsMale] AS [ptIsMale], 
    [Extent1].[ptUserID] AS [ptUserID], 
    [Extent1].[ptINN] AS [ptINN], 
    [Extent1].[ptSNILS] AS [ptSNILS], 
    (SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[tblEvent] AS [Extent2]
        WHERE ([Extent1].[ptID] = [Extent2].[evPatientID]) AND ([Extent2].[evEventKindID] = @p__linq__0)) AS [C1]
    FROM [dbo].[tblPatient] AS [Extent1]
)  AS [Project1]
WHERE [Project1].[C1] > 0',N'@p__linq__0 int',@p__linq__0=29

And in the place {2} on the second iteration:

exec sp_executesql N'SELECT 
[Project2].[ptID] AS [ptID], 
[Project2].[ptFullName] AS [ptFullName], 
[Project2].[ptHomeAddress] AS [ptHomeAddress], 
[Project2].[ptPhone] AS [ptPhone], 
[Project2].[ptBirthDate] AS [ptBirthDate], 
[Project2].[ptIsMale] AS [ptIsMale], 
[Project2].[ptUserID] AS [ptUserID], 
[Project2].[ptINN] AS [ptINN], 
[Project2].[ptSNILS] AS [ptSNILS]
FROM ( SELECT 
    [Project1].[ptID] AS [ptID], 
    [Project1].[ptFullName] AS [ptFullName], 
    [Project1].[ptHomeAddress] AS [ptHomeAddress], 
    [Project1].[ptPhone] AS [ptPhone], 
    [Project1].[ptBirthDate] AS [ptBirthDate], 
    [Project1].[ptIsMale] AS [ptIsMale], 
    [Project1].[ptUserID] AS [ptUserID], 
    [Project1].[ptINN] AS [ptINN], 
    [Project1].[ptSNILS] AS [ptSNILS], 
    (SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[tblEvent] AS [Extent3]
        WHERE ([Project1].[ptID] = [Extent3].[evPatientID]) AND ([Extent3].[evEventKindID] = @p__linq__1)) AS [C1]
    FROM ( SELECT 
        [Extent1].[ptID] AS [ptID], 
        [Extent1].[ptFullName] AS [ptFullName], 
        [Extent1].[ptHomeAddress] AS [ptHomeAddress], 
        [Extent1].[ptPhone] AS [ptPhone], 
        [Extent1].[ptBirthDate] AS [ptBirthDate], 
        [Extent1].[ptIsMale] AS [ptIsMale], 
        [Extent1].[ptUserID] AS [ptUserID], 
        [Extent1].[ptINN] AS [ptINN], 
        [Extent1].[ptSNILS] AS [ptSNILS], 
        (SELECT 
            COUNT(1) AS [A1]
            FROM [dbo].[tblEvent] AS [Extent2]
            WHERE ([Extent1].[ptID] = [Extent2].[evPatientID]) AND ([Extent2].[evEventKindID] = @p__linq__0)) AS [C1]
        FROM [dbo].[tblPatient] AS [Extent1]
    )  AS [Project1]
    WHERE [Project1].[C1] > 0
)  AS [Project2]
WHERE [Project2].[C1] > 0',N'@p__linq__0 int,@p__linq__1 int',@p__linq__0=31,@p__linq__1=31

What do you think about this problem?

  • 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-10T15:54:06+00:00Added an answer on June 10, 2026 at 3:54 pm

    This is a common confusion with foreach. Queries that refer to variables get their parameter values when the query is executed, not when the parameter is bound. So you can have

    int orderId = 1;
    var query = from o in context.Orders where o.Id == orderId;
    orderId = 2;
    MessageBox.Show(query.Single().Id.ToString()); // shows that order 2 was retrieved
    

    In your case, your foreach loop has one variable limit. You refer to it multiple times, but those multiple references all see the same value. Which is why you see

    N'@p__linq__0 int,@p__linq__1 int',@p__linq__0=31,@p__linq__1=31
    

    Both parameters have the value 31, the 29 from the first iteration is gone.

    The easiest way around this is to create a new variable each time:

    foreach (var limit in group.limits.Values)
    {
        var locallimit = limit;
        // refer to locallimit in your query, not to limit
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this database table: id | url ----------------------------------------- 1 | http://stackoverflow.com/ 2 |
i want to make a text box like stackoverflow have for the tag at
[i meant to ask this question on StackOverflow...] I have been using VisualSVN Server
StackOverflow - I'm still trying to deploy this site, but with every problem I
StackOverflow - I'm still trying to deploy this site, but with every problem I
I have a varbinary field in my sql server database that needs to be
I have a decision to make and I'm kicking it to the stackoverflow community.
Based on the answer I received on this question ( https://stackoverflow.com/questions/1911969/... ) I have
I have a StackOverflow-like tagging system for a database I'm working on. And I'm
I have searched on google and stackoverflow for this problem there are several questions

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.