Where Operator in LinqToSql

Where operator comes under the category of Restriction operator. It use when we want information on based on condition. Suppose we want products that have weight more than 100gm. That for we use Where operator for filtering rows. So there are step first Create a list than use that list with Where operator:

Step 1 Create a generic list

List collection in the .NET Framework, which is located in the System.Collections.Generic namespace. We can say that Lists are dynamic array in C#. we need < and > for list declaration. Advantage of List<T> comparison ArrayList is that List<T> can grow and shrinks as the number of stored objects changes.

Illustrate with an Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace RestrictionOperator
{
    public class Product
    {
        //Variable declaration
        string m_produtct = string.Empty;
        int m_unit = 0;

        /// <summary>
        /// Name of Product
        /// </summary>
        public string ProductName
        {
            set { m_produtct = value; }
            get { return m_produtct; }
        }

        /// <summary>
        /// How many units of products
        /// </summary>
        public int Unit
        {
            set { m_unit = value; }
            get { return m_unit; }
        }

        /// <summary>
        /// Constructor of Product class That contain Name and Units of a product
        /// </summary>
        /// <param name="ProductName"></param>
        /// <param name="Unit"></param>
        public Product(string ProductName, int Unit)
        {
            this.ProductName = ProductName;
            this.Unit = Unit;

        }
        /// <summary>
        /// Another Constrator that contain only Product name
        /// </summary>
        /// <param name="ProductName"></param>
        public Product(string ProductName)
        {
            this.ProductName = ProductName;
        }

        /// <summary>
        /// Genarte a list of product with or without quantity
        /// </summary>
        /// <returns></returns>
        public static List<Product> GetProduct()
        {
            List<Product> ProductList = new List<Product>();//Name of product List
            ProductList.Add(new Product("Book", 1));//Add product with Unit
            ProductList.Add(new Product("Pen"));//Add product without unit
            ProductList.Add(new Product("Pensil"));
            ProductList.Add(new Product("Paper", 20));
            return ProductList;

        }

     }
}

Step 2: Using Where operator

Here we access our previous create List and apply Where operator for access products.

Illustrate with an Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace RestrictionOperator
{
    public partial class ListExample : System.Web.UI.Page
    {       
       
protected void Page_Load(object sender, EventArgs e)
        {
            List<Product> produ = Product.GetProduct();//Create product list
            var prod = from pro in produ where pro.Unit>0 select pro;/* where condition Check product unit
                                                                                 * * greater than 0*/
            //Retrive product information
            foreach (var p in prod)
            {
                Response.Write("<b>Product Name</b> " + p.ProductName +"  <b> Product Unit :</b>"+p.Unit+ "<br/>");
            }
        }
    }
}

Output:

Product Name Book Product Unit :1


Product Name Paper Product Unit :20



Next Recommended Reading LINQ : Standard Query Operators