Solved

Using List in C#

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 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