Question: I have a list of Object **Product** :

```
public Class Product 
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Status { get; set; }
}

```

I have a list of the above product :

```
List<Product> p ;

```

I am trying to filter this List , based on Search Criteria `ID, Title, Status.`

```
Example :
Id Title  Status
1  ABC   OPEN
2  CDE   CLOSED
3  FGH   RESOLVED
4  IJK   PROPOSED
5  LMN   SET
6  OPQ   CLOSED
7  MNO   OPEN
8  STU   CLOSED.

```

If Search Fields **ID 1** is entered : It should return 

```
1  ABC   OPEN

```

If search Fields **Status OPEN** is entered : It should return

```
1  ABC   OPEN
7  MNO   OPEN

```

If Search Fields **Title "MNO"** and **Status "OPEN"** are entered it should return :
 **7 MNO OPEN**

If **Id = ""** and **Title = ""** and **Status = "ALL"** it should return all the elements.

I am going to bind this List of objects to an asp.net grid view.

So far the code I have is below : 

```
var results = new List<Product>();

foreach (var prod in Product)
{
    if (prod.ID == txtProd)
    {
        results.Add(prod);
    }
    if (prod.Title.ToUpper() == txtTitle)
    {
        results.Add(prod);
    }
    if (prod.Status.ToUpper() == strStatus)
    {
        results.Add(prod);
    }
}

```

Can you please tell me how I can modify this code to achieve my results ?

Answer: 
Basically you want to use `AND` between your conditions, what you are currently doing is an OR, and you will add the same item twice when it satisfies more than one condition.

You could built a dynamic linq query based on parameters:

```
var query = Product.AsQueryable();
query = txtProd != string.Empty ?  query.Where(x => x.ID == txtProd) : query;
query = txtTitle != string.Empty ? query.Where(x => x.Title == txtTitle) : query;
query = strStatus != string.Empty ? query.Where(x => x.Status == strStatus) : query;

var results = query.ToList();

```
```
    var results = new List < Product > ();
    if (strStatus == "ALL" || txtProd == "" || txtTitle == "")
        results = p;
    else
        foreach(var prod in p) {
            if (prod.ID == Int32.Parse(txtProd)) {
                results.Add(prod);
            }
            if (prod.Title.ToUpper() == txtTitle) {
                results.Add(prod);
            }
            if (prod.Status.ToUpper() == strStatus) {
                results.Add(prod);
            }
        }
    results = results.Distinct()
        .ToList();

```