Solved

Using List in C#

Posted on 2015-01-11
4
141 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 62

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now