C# – How to use or operator in linq to sql

clinqnet

I am using this code but getting an error. I want to use an OR operator.

DataClasses1DataContext dc = new DataClasses1DataContext();

private void button4_Click(object sender, EventArgs e)
{
   var i = dc.vins
             .Where(aa => aa.startDate < DateTime.Now)
             .Where(aa => aa.Sno > 1)
             .Select(aa => aa);
   dataGridView1.DataSource = i;
}

This code is working as an "AND" operator how can I have it act as an "OR" operator?

Best Answer

Just put both conditions in one Where call, and use the normal C# || operator:

var i = dc.vins.Where(aa => aa.startDate < DateTime.Now || aa.Sno > 1);

Note that I've also removed your Select call as it wasn't doing anything useful.

Note that depending on your exact scenario, you may well want to use DateTime.UtcNow instead of DateTime.Now; you should carefully consider how you want time zones to be handled.