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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:53:47+00:00 2026-06-01T12:53:47+00:00

I Have following code with LINQ var q = (from web in DataContext.Webs select

  • 0

I Have following code with LINQ

var q = (from web in DataContext.Webs select web);
List<int?> k1 = new List<int?>() { 1, 2 };
List<int?> k2=new List<int?>() { 16, 17 };
            q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId)));
            System.Diagnostics.Debug.WriteLine(q.Count());
            k1 = k2;
            q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId)));
            System.Diagnostics.Debug.WriteLine(q.Count());

Here Is what query it generates.

First Execution of count.

SELECT COUNT(*) AS [value]
FROM [dbo].[Web] AS [t0]
WHERE EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[WebTechMap] AS [t1]
    WHERE ([t1].[WebsiteId] = ([t0].[WebsiteId])) AND ([t1].[TechId] IN (@p0, @p1))
    )
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [2]

Second Execution of count function

SELECT COUNT(*) AS [value]
FROM [dbo].[Web] AS [t0]
WHERE (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[WebTechMap] AS [t1]
    WHERE ([t1].[WebsiteId] = ([t0].[WebsiteId])) AND ([t1].[TechId] IN (@p0, @p1))
    )) AND (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[WebTechMap] AS [t2]
    WHERE ([t2].[WebsiteId] = ([t0].[WebsiteId])) AND ([t2].[TechId] IN (@p2, @p3))
    ))
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [17]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [17]

Please Take a look at the parameters passed to query.

Seems it uses the new referenced object for both Expressions.

Somehow @p0 and @p1 change their old values.
I understand that issue is in Linq it uses new referenced object.

-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [2]



-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [17]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [17]

Can someone explain how to use same name of List object but keep the query using correct List’s

Desired output is

-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [2]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [17]

Thanks in Advance

  • 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-01T12:53:48+00:00Added an answer on June 1, 2026 at 12:53 pm

    Your first query expression uses k1. It uses the variable kl, and that’s what’s captured. When you change the value of k1, that’s effectively changing the meaning of the query. If you don’t want the meaning of the query to change, don’t change the captured variables! Just use k2 in your second filter:

    var q = (from web in DataContext.Webs select web);
    List<int?> k1 = new List<int?>() { 1, 2 };
    List<int?> k2=new List<int?>() { 16, 17 };
    q = q.Where(web => DataContext.WebTechMaps
                                  .Any(t => t.WebsiteId == web.WebsiteId && 
                                            k1.Contains(t.TechId)));
    System.Diagnostics.Debug.WriteLine(q.Count());
    q = q.Where(web => DataContext.WebTechMaps
                                  .Any(t => t.WebsiteId == web.WebsiteId &&
                                       k2.Contains(t.TechId)));
    System.Diagnostics.Debug.WriteLine(q.Count());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var columnNames = (from autoExport in dataContext.AutoExports where autoExport.AutoExportTemplate
I have the following LINQ code: var posts = (from p in db.Posts .Include(Site)
I have the following code: ServiceSoapClient service = new ServiceSoapClient(); var dataSource = (from
I have the following code: var personIds = from player in myPlayers select player.person_id;
I have the following code: using (var db = new IntDB()) { var subscription
I have the following code which builds a Generic List of Abc from a
I have the following code: var thing = (from t in things where t.Type
I had the following Linq code: var allRequests = model.GetAllRequests(); var unsatisifedRequests = (from
i have written the following code var resumeedit=(from t in db.Resumes where t.User.UserID==theUserID &&
I have the following code in my controller: public ActionResult Details(int id) { var

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.