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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:08:42+00:00 2026-06-09T13:08:42+00:00

I have a DataTable that is populated from a database as follows: SPName SName

  • 0

I have a DataTable that is populated from a database as follows:

SPName      SName   Row  text
p_obj_prod1 dbo     1    A
p_obj_prod1 dbo     1    B
p_obj_prod1 dbo     1    C
p_obj_prod2 dbo     1    D
p_obj_prod2 dbo     1    E
p_rpt_prod3 dbo     1    F
p_rpt_prod3 dbo     1    G
p_rpt_prod3 dbo     1    H

And I have the following code that will process this data:

foreach (var row in procedureData.Tables[0].AsEnumerable()
    .Select(dr => new { Schema = dr["SName"], Procedure = dr["SPName"] })
    .Distinct())
{
     //DO STUFF
     foreach (string procedureText in procedureData.Tables[0].AsEnumerable()
         .Where(dr => dr["SPName"] == row.Procedure).Select(dr => dr["text"]))
     {
          //DO MORE STUFF
     }  
     //DO EVEN MORE STUFF
 }

In this case I would expect the outer foreach loop to iterate 3 times, and it does. I would also expect that the inner foreach loop would iterate 3 times for the first outer loop iteration, 2 times for the second and 3 times for the third. However the inner loop only ever has one value (the first one in the sequence).

I’m not new to LINQ and have create far more complex queries than this, but this one really has me stumped on the reason why the inner loop doesn’t have all the expected results. I guess I have made a mistake in the second loop’s LINQ query, but it seems fine to me.

Can anyone shed some light on what might be the issue?

Thanks very much.

UPDATE

Just realised the mistake I was making and why the inner loop only had one value. It was because it was doing a reference comparison, not a value comparison on the ‘SPName’ column of the DataTable.

Changing the inner loop’s code to the following fixed the issue, however I will keep the GroupBy version as I prefer it!

foreach (string procedureText in procedureData.Tables[0].AsEnumerable()
         .Where(dr => dr["SPName"].ToString() == row.Procedure.ToString())
         .Select(dr => dr["text"]))
     {
  • 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-09T13:08:44+00:00Added an answer on June 9, 2026 at 1:08 pm

    Why not just use a GroupBy:

    var grouped = procedureData.Tables[0]
                               .AsEnumerable()
                               .GroupBy(dr=>new 
                               { 
                                    Schema = dr["SName"], 
                                    Procedure = dr["SPName"] 
                               });
    
    foreach(var g in grouped)
    {
        //DO STUFF
        foreach(string procedureText in g.Select(dr => dr["text"]))
        {
            //DO MORE STUFF
        }
        //DO EVEN MORE STUFF
    }
    

    Otherwise, I don’t see what’s wrong with your code. It works fine for me.

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

Sidebar

Related Questions

I have a .net/ c# console app with a DataTable that I populated from
I have a datatable, transformersDT, populated with data from a database table. I'd like
I have a datatable that uses the cell editor function. In each row, I
I am having a Datagrid that gets populated with values from a DataTable. In
my dropdownlist is populated from the database as in: DataTable dt = GetData(); ddlMylist.DataSource
I have a DataGridView bound to a DataTable. The DataTable is populated from a
I am getting data from my database and I want to have that data
I have a WPF DataGrid that will be populated from a data table having
I have a datatable object, which is populated from a webservice. Apparently, the web
I have a DataViewGrid I populated from a DataTable in a query to a

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.