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

  • Home
  • SEARCH
  • 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 171227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:57:01+00:00 2026-05-11T12:57:01+00:00

I have the following code: var tagToPosts = (from t2p in dataContext.TagToPosts join t

  • 0

I have the following code:

 var tagToPosts = (from t2p in dataContext.TagToPosts                                     join t in dataContext.Tags on t2p.TagId equals t.Id                                     select new { t2p.Id, t.Name }); //IQueryable tag2postsToDelete; foreach (Tag tag in tags) {     Debug.WriteLine(tag);     tagToPosts = tagToPosts.Where(t => t.Name != tag.Name); } IQueryable tagToPostsToDelete = (from t2p in dataContext.TagToPosts                                             join del in tagToPosts on t2p.Id equals del.Id                                             select t2p); dataContext.TagToPosts.DeleteAllOnSubmit(tagToPostsToDelete); 

where tags is a List<Tag> – list of tags created by constructor, so they have no id. I run the the code putting a brakepoint on line with Debug and wait for a few cycles to go. Then I put tagToPosts.ToList() in the Watch window for query to execute. In SQL profiler I can see the following query:

exec sp_executesql N'SELECT [t0].[Id], [t1].[Name] FROM [dbo].[tblTagToPost] AS [t0] INNER JOIN [dbo].[tblTags] AS [t1] ON [t0].[TagId] = [t1].[Id] WHERE ([t1].[Name]  @p0) AND ([t1].[Name]  @p1) AND ([t1].[Name]  @p2)',N'@p0 nvarchar(4),@p1 nvarchar(4),@p2 nvarchar(4)',@p0=N'tag3',@p1=N'tag3',@p2=N'tag3' 

We can see every parameter parameter have the value of last tag.Name in a cycle. Have you any idea on how to get arout this and get cycle to add Where with new condition every tyme? I can see IQueryable stores only pointer to variable before execution.

  • 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. 2026-05-11T12:57:02+00:00Added an answer on May 11, 2026 at 12:57 pm

    Change your foreach to:

    foreach (Tag tag in tags){     var x = tag.Name;     tagToPosts = tagToPosts.Where(t => t.Name != x); } 

    The reason behind this is lazy evaluation and variable capturing. Basically, what you are doing in the foreach statement is not filtering out results as it might look like. You are building an expression tree that depends on some variables to execute. It’s important to note that the actual variables are captured in that expression trees, not their values at the time of capture. Since the variable tag is used every time (and it’s scope is the whole foreach, so it won’t go out of scope at the end of each iteration), it’s captured for every part of the expression and the last value will be used for all of its occurrences. The solution is to use a temporary variable which is scoped in the foreach so it’ll go out of scope at each iteration and it the next iteration, it’ll be considered a new variable.

    • 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 searcher = new ManagementObjectSearcher(SELECT Name, Path FROM Win32_share);
I have the following code: var resultArticles = from a in articleItems select new
I have the following code: var foo = (from data in pivotedData.AsEnumerable() select new
I have the following code: var columnNames = (from autoExport in dataContext.AutoExports where autoExport.AutoExportTemplate
I have the following code: var categories = catList.Select(c => new { Title =
I have the following code: var personIds = from player in myPlayers select player.person_id;
I have the following code: var json = MyObject .Select(p => new { id
I have the following code: var allWorkorders = (from wo in context.WORKORDERs join wot
I have the following code: var newDate=new Date('05/22/2012'); var month=newDate.getMonth(); var day=newDate.getDate()+(-2); var year=newDate.getYear();
I have the following code: var tlp = new TableLayoutPanel { Location = new

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.