Solved

Using List in C#

Posted on 2015-01-11
4
156 Views
Last Modified: 2015-01-11
Hi ,

I have a following requreiment in C# using list, I will get a single row with 22 columns (always returns even number of columns) from database.The first column contain From range value and second column contain To range value and so on...Please see below example

Ex:

select col_from1,col_to1,col_from2,col_to2,col_from3,col_to3------etc (22 columns)

100,150,200,299,300,350,400,420,500,570,0,0,500,580,601,699,700,800,0,0,0,0 (22 columns)

From   TO (22 columns)

100 - 150
200 - 299
300 - 350
400 - 420
500 - 570
0   - 0
500 - 580
601 - 699
700 - 800
0   - 0
0   - 0

I need to find out if a given number exist within the range or not.Initially I thought of having dictionary but the range may contain duplicate values. So I must use List.That means the list will have  first item(From range), second item(To range),third item(From range), fourth item(To range) and so on.

My requirement is while reading values from database I need to ignore 0's and store all other values in the list ,in the above example we should get 16 items since in the list  there are 6 items with 0's and then find if a given number (Ex:900) exist in the list of range or not.

How can I achieve the above requriement using List in c#  and looking for sample code.

Thanks in Advance
0
Comment
Question by:ksd123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 40543129
Here's a really quick program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int testNumber = 340;
            var L = new List<int>() { 100, 150, 200, 299, 300, 350, 400, 420, 500, 570, 0, 0, 500, 580, 601, 699, 700, 800, 0, 0, 0, 0 };
            for (int i = 0; i < L.Count; i += 2)
            {
                if (L[i] == 0) continue;
                if (testNumber > L[i] && testNumber < L[i+1])
                    textBox1.Text += testNumber + " falls between " + L[i] + " and " + L[i+1];
            }
        }
    }
}

Open in new window

0
 

Author Comment

by:ksd123
ID: 40543154
I gave  testnumber as 800, it's not working.

I noticed the above logic loops thru all the items in the list  even if  given number falls at first item range for ex I gave 100 and it falls in the first item range but it is looping all the remaing items . It should exit the loop if it find's the given number. It should also ignore 0's in the list.
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 400 total points
ID: 40543167
It should ignore zeros already; I didn't realize that you want to break out of the loop if one of the ranges is found. Try this.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int testNumber = 340;
            var L = new List<int>() { 100, 150, 200, 299, 300, 350, 400, 420, 500, 570, 0, 0, 500, 580, 601, 699, 700, 800, 0, 0, 0, 0 };
            for (int i = 0; i < L.Count; i += 2)
            {
                if (L[i] == 0) continue;
                if (testNumber > L[i] && testNumber < L[i + 1])
                {
                    textBox1.Text += testNumber + " falls between " + L[i] + " and " + L[i + 1];
                    break;
                }
            }
        }
    }
}

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 40543177
Hi ksd123;

I think this may result in something that you can use to solve you requirements.

// Test data returned from the database in a single row.
List<int> theList = new List<int>() { 100, 150, 200, 299, 300, 350, 400, 420, 500, 570, 0, 0, 500, 580, 601, 699, 700, 800, 0, 0, 0, 0 };
// Convert the List into a List holding a Tuple of two integers From, To
List<Tuple<int, int>> fromToList = new List<Tuple<int, int>>();

for (int i = 0; i < theList.Count(); i += 2)
{
    // Remove rows that have a From 0 To 0
    if ((theList[i] == 0) && (theList[i + 1] == 0)) continue;
    fromToList.Add(new Tuple<int, int>(theList[i], theList[i+1]));
}
// Test data to look and see if it is in any of the ranges
int target = 250;
// The test to see in the target value is in one of the ranges
bool result = fromToList.Where(ftl => ftl.Item1 <= target && 
                                      ftl.Item2 >= target).Any();

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question