Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using List in C#

Posted on 2015-01-11
4
Medium Priority
?
170 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 1600 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 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