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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:32:08+00:00 2026-06-18T12:32:08+00:00

I query a two tables from a database and add unique values to a

  • 0

I query a two tables from a database and add unique values to a generic list.

If there is a value that I do not want to add to the list, how can I prevent the item from being added?

using (myOledbConn = new OleDbConnection(connAccessLrProduct))
        {
            List<string> lst = new List<string>();
            myOledbConn.Open();
            OleDbCommand cmd = myOledbConn.CreateCommand();
            cmd.CommandText = @"SELECT tblProducts.CODE, tblSubject.SUBJECT, tblProducts.GenSubject
                               FROM tblSubject INNER JOIN tblProducts ON tblSubject.ID = tblProducts.SubjectID
                               WHERE [SUBJECT] = 'Arts' or [SUBJECT] = 'Aged Care';";


            OleDbDataReader dbReader = cmd.ExecuteReader();
            while (dbReader.Read())
            {
                string generalSublject;
                string subject = (string)dbReader["SUBJECT"];
                if (lst.Where(t => t == subject).Count() == 0)
                    lst.Add(subject);
                if (dbReader["GenSubject"] != DBNull.Value)
                {
                    generalSublject = (string)dbReader["GenSubject"];
                    if(generalSublject.Equals("No related topics"))
                    {
                        //how do I exclude this item from being added to the list?
                    }
                    if (lst.Where(t => t == generalSublject).Count() == 0)
                        lst.Add(generalSublject);
  • 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-18T12:32:10+00:00Added an answer on June 18, 2026 at 12:32 pm

    I’d suggest you to use a ISet<T> instead of a List<T>, which makes elements unique for you. You can then decide whether or not to add an element by an if.

    var mySet = new SortedSet<string>();
    
    while(dbReader.Read())
    {
      if(dbReader["GenSubject"] != DBNull.Value)
      {
        var generalSubject = (string)dbReader["GenSubject"];
        if(!generalSublject.Equals("No related topics"))
        {
          mySet.Add(generalSubject); // returns false if already in Set
        }
        else
        {
          // do nothing
        }
    }
    

    Did this help you and answer your question? I hope I got you right and helped you with a simplified version of the code that focuses on the problem only. It has a few implications though:

    • An ISet – typical implementations are SortedSet (tree based) and HashSet (hash based) – does not guarantee a particular order of the elements, but it does guarantee they’re unique.
    • You can do a Contains on an ISet with (more or less) logarithmic rather than linear effort (might speed up your program measurably, depending on the number of elements).
    • You might also go for an SQL DISTINCT to make elements unique if suitable. However, this requires you to refactor the query.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an INSERT query that is pulling data from two tables and inserting
I have used a join query for retrieving value from two tables one is
I've currently got a query that selects metrics data from two tables whilst getting
I have a database that have two tables. The sql query fetching data correctly.
I have two tables that I'm querying from. In one table, there are fields
I have two tables in my database that Im trying to use one Query
I have simple query that loads data from two tables into GUI. I'm saving
I have the following query where I combine two fields from two tables: <cfquery
I have the query to select some columns from two tables, which must contain
I was using the exact same query to get data from two tables. when

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.