none
LINQ: search value between two numbers RRS feed

  • Question

  • I'm using VS 2008 & LINQ. There are two textbox in a web form, "id=textFrom" and "id=textTo". If I type 5 at textFrom and type 12 at textTo. How to get the result at 5,6,7,8,9,10.11.12 ? Thank you!

    This is my code for search the product name from a table.

    Assumption: I want to search the "Product_Id" in the table of "Productlist".

    protected void btnSearch_Click(object sender, EventArgs e)

        {

            using (DataLayerDataContext dc = new DataLayerDataContext())

            {

                var qry = from Productlist in dc.Productlists

                          select Productlist;

                qry = qry.Where(p => p.Product_Name.StartsWith(textName.Text));

                grdResult.DataSource = qry;         

                grdResult.DataBind();

     

            }

        }

     

    Friday, March 25, 2011 2:55 PM

Answers

  • Hi Gordon;

    You can combine you query as follows to achieve what you need.

    protected void btnSearch_Click(object sender, EventArgs e)
    {
      using (DataLayerDataContext dc = new DataLayerDataContext())
      {
    
        int from = int.Parse( textFrom.Text );
        int to = int.Parse( textTo.Text );
    
        var qry = from Productlist in dc.Productlists
             where Productlist.Product_Name.StartsWith(textName.Text) 
               && Productlist.Id >= from && Productlist.Id <= to
             select Productlist; 
    
        grdResult.DataSource = qry;     
        grdResult.DataBind(); //output result to gridview
    
      }
    }
    

    Fernando
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, March 27, 2011 4:53 PM

All replies

  • In some more easy way, you can do it like:

          int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
          var undefined = from a in array
                  where a > 4 && a < 13
                  select a;
          foreach (int a in undefined)
            Console.WriteLine("{0}", a);
          Console.ReadLine();
    
    Mitja

    Friday, March 25, 2011 5:28 PM
  • Hi Gordon;

    To this part of your question, "I'm using VS 2008 & LINQ. There are two textbox in a web form, "id=textFrom" and "id=textTo". If I type 5 at textFrom and type 12 at textTo. How to get the result at 5,6,7,8,9,10.11.12 ? Thank you!", using the Linq Range method you can get the results you want.

    int from = int.Parse( textFrom.Text );
    int to = int.Parse( textTo.Text );
    
    List<int> seq = Enumerable.Range( from, to - from + 1 ).ToList( );
    


    As far as the code you posted I am not sure what you are asking.

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, March 25, 2011 8:25 PM
  • Hi Fernandoo,

    protected void btnSearch_Click(object sender, EventArgs e)

        {

            using (DataLayerDataContext dc = new DataLayerDataContext())

            {

                var qry = from Productlist in dc.Productlists

                          select Productlist;

                qry = qry.Where(p => p.Product_Name.StartsWith(textName.Text)); //criteria: search product name in Textbox(textName)

                grdResult.DataSource = qry;         

                grdResult.DataBind(); //output result to gridview

            }

        }

     

    Now, i want set a new criteria for search the value between "id=textFrom" and "id=textTo".

    Thank you!

    Sunday, March 27, 2011 7:36 AM
  • Your way of using "StartWith" operation will do Nothing. This will only get the names (or what ever) which starts on this letter. But I think you would like to get items which stats from this letter and on. On the other side you have a limitation to where the query can go; this is the textBox2. So query goes from textBox1 to textBox2. The query will get all the values in between these two statements.

    This is the full solution for you:

      public partial class Form1 : Form
      {
        List<ProductList> list;
        public Form1()
        {
          InitializeComponent();
    
          list = new List<ProductList> 
          {
           //EXAMPLE CODE TO FILL THE LIST:
           new ProductList { ProductID = 1, ProductName = "Motherboard" }, 
           new ProductList { ProductID = 2, ProductName = "KeyBoard" }, 
           new ProductList { ProductID = 3, ProductName = "Graphics card" }, 
           new ProductList { ProductID = 4, ProductName = "Hard drive" },        
           new ProductList { ProductID = 5, ProductName = "Floppy drive" }, 
           new ProductList { ProductID = 6, ProductName = "DVD - ROM" }, 
           new ProductList { ProductID = 7, ProductName = "RAM" },
           new ProductList { ProductID = 8, ProductName = "CPU" } 
          };
        }
    
        bool bNotShowing;
        private void button1_Click(object sender, EventArgs e)
        {
          var newList = list.Where(w => w.ProductName.CompareTo(textBox1.Text) >= 0 &&
                          w.ProductName.CompareTo(textBox2.Text) <= 0).
                    OrderBy(o => o.ProductName).
                    Select(s => s.ProductName).ToList();
          bNotShowing = true;
          listBox1.DataSource = newList;
          listBox1.DisplayMember = "ProductName";
          listBox1.ValueMember = listBox1.DisplayMember;
        }
    
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          if (listBox1.SelectedIndex > -1 && !bNotShowing)
          {
            MessageBox.Show("Selected item is: " + listBox1.SelectedItem.ToString() + ".");
          }
          else
            bNotShowing = false;
        }
      }
    
      class ProductList
      {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
      }

    I did all my best, I hope you like it,

    Mitja


    Sunday, March 27, 2011 2:14 PM
  • This one is even better, concernng the query. It gets th whole ProductList class, so we can retreive the properties of the class (ProductID and ProductName) in the query.

    Thats why I can get them out both, when selecting an item in the listBox:

    public partial class Form1 : Form
      {
        List<ProductList> list;
        public Form1()
        {
          InitializeComponent();
    
          list = new List<ProductList> 
          {
           new ProductList { ProductID = 1, ProductName = "Motherboard" }, 
           new ProductList { ProductID = 2, ProductName = "KeyBoard" }, 
           new ProductList { ProductID = 3, ProductName = "Graphics card" }, 
           new ProductList { ProductID = 4, ProductName = "Hard drive" },        
           new ProductList { ProductID = 5, ProductName = "Floppy drive" }, 
           new ProductList { ProductID = 6, ProductName = "DVD - ROM" }, 
           new ProductList { ProductID = 7, ProductName = "RAM" },
           new ProductList { ProductID = 8, ProductName = "CPU" } 
          };
        }
    
        bool bNotShowing;
        private void button1_Click(object sender, EventArgs e)
        {
          var newList = list.Where(w => w.ProductName.CompareTo(textBox1.Text) >= 0 &&
                          w.ProductName.CompareTo(textBox2.Text) <= 0).
                    OrderBy(o => o.ProductName).
                    Select(s => new ProductList { ProductID = s.ProductID, ProductName = s.ProductName }).ToList(); //THIS IS CHANGED!!!
          bNotShowing = true;
          listBox1.DataSource = newList;
          listBox1.DisplayMember = "ProductName";
          listBox1.ValueMember = "ProductID";
          bNotShowing = false;
        }
    
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          if (listBox1.SelectedIndex > -1 && !bNotShowing)
          {
            int id = Convert.ToInt32(listBox1.SelectedValue);
            string name = listBox1.Text;
            MessageBox.Show("Selected item:\nID: " + id + "\nNAME: " + name);
          }      
        }
      }
    
      class ProductList
      {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
      }
    

     

    Mitja

    Sunday, March 27, 2011 2:39 PM
  • Hi Gordon;

    You can combine you query as follows to achieve what you need.

    protected void btnSearch_Click(object sender, EventArgs e)
    {
      using (DataLayerDataContext dc = new DataLayerDataContext())
      {
    
        int from = int.Parse( textFrom.Text );
        int to = int.Parse( textTo.Text );
    
        var qry = from Productlist in dc.Productlists
             where Productlist.Product_Name.StartsWith(textName.Text) 
               && Productlist.Id >= from && Productlist.Id <= to
             select Productlist; 
    
        grdResult.DataSource = qry;     
        grdResult.DataBind(); //output result to gridview
    
      }
    }
    

    Fernando
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Sunday, March 27, 2011 4:53 PM
  • Thank you for your help!
    Tuesday, March 29, 2011 3:07 PM
  • Not a problem, glad I was able to help.
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, March 29, 2011 3:57 PM