Solved

Using List in C#

Posted on 2015-01-11
4
153 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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