user2266684
user2266684

Reputation: 1

How to use or operator in linq to sql

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?

Upvotes: 0

Views: 9467

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1500065

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.

Upvotes: 7

John Woo
John Woo

Reputation: 263693

how about this?

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

and actually your AND condition should look like this,

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

.Select() is optional since you have not done any calculation or modification on aa.

Upvotes: 4

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

Simply use the || inside your WHERE clause

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

Upvotes: 1

Darren
Darren

Reputation: 70718

The OR operator is ||, therefore you can try:

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

Upvotes: 0

Bala R
Bala R

Reputation: 108937

Use

.Where(aa => aa.startDate < DateTime.Now || aa.Sno > 1 ).Select...

Upvotes: 1

Related Questions