LINQ (C# 3.0)
-
I'm trying to do the equivalent of this in LINQ:
StringBuilder q = new StringBuilder("SELECT * FROM customer WHERE city = 'New York'");
if (westCoast)
q.Append(" OR city = 'Los Angeles'");Here's what I have so far:
var q = dc.Customer.Where(c => c.City == "New York");
if (westCoast)
q.Where(c => c.City == "Los Angeles");But I end up with "WHERE city = 'New York' AND city = 'Los Angeles'"; How do I tell it to use OR instead? Thanks, Alvaro
God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet
-
I'm trying to do the equivalent of this in LINQ:
StringBuilder q = new StringBuilder("SELECT * FROM customer WHERE city = 'New York'");
if (westCoast)
q.Append(" OR city = 'Los Angeles'");Here's what I have so far:
var q = dc.Customer.Where(c => c.City == "New York");
if (westCoast)
q.Where(c => c.City == "Los Angeles");But I end up with "WHERE city = 'New York' AND city = 'Los Angeles'"; How do I tell it to use OR instead? Thanks, Alvaro
God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet
Hi Alvaro. I haven't used LINQ too much, but I understand the Where method takes a Predicate<Customer>. All you're doing is writing a lambda expression, kind of shorthand for an anonymous method. Here's a more verbose version that should work, assuming Where takes a predicate:
var q = dc.Customer.Where(Criteria);
...
bool Criteria(Customer input)
{
return input.City == "New York" || (westCoast && input.City == "Los Angeles");
}I'm not sure, but you may be able to do this right inside the lambda:
var q = dc.Customer.Where(c => c.City == "New York" || (westCoast && c.City == "Los Angeles"));
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi Alvaro. I haven't used LINQ too much, but I understand the Where method takes a Predicate<Customer>. All you're doing is writing a lambda expression, kind of shorthand for an anonymous method. Here's a more verbose version that should work, assuming Where takes a predicate:
var q = dc.Customer.Where(Criteria);
...
bool Criteria(Customer input)
{
return input.City == "New York" || (westCoast && input.City == "Los Angeles");
}I'm not sure, but you may be able to do this right inside the lambda:
var q = dc.Customer.Where(c => c.City == "New York" || (westCoast && c.City == "Los Angeles"));
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
Thanks for your reply Judah. I was more looking to do this in two steps, as an exercise. The first step sets up the base query, and then it would start growing based on different criteria. As I discovered, the default behavior is to AND the conditions together on multiple calls to
Where
. I was hoping someone would know how do change that to use OR instead. Regards, Alvaro
God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet
-
Thanks for your reply Judah. I was more looking to do this in two steps, as an exercise. The first step sets up the base query, and then it would start growing based on different criteria. As I discovered, the default behavior is to AND the conditions together on multiple calls to
Where
. I was hoping someone would know how do change that to use OR instead. Regards, Alvaro
God existing isn't entirely impossible, but there's absolutely no evidence for it, so... the personal God as described by the Christian Bible existing is just as likely as a Pink Unicorn having created the universe, oh.. say... last Thursday. It's equally possible the moon has a core made of cheese. It's equally possible this sentence is in Spanish when you're not looking. - Someone on the Internet
I'm afraid I don't know how to do in 2 seperate pieces rather than 1 query with multiple conditions. Have you tried asking in the MSDN forums? I'm thinking too few people here have played with LINQ, so it might be tough getting an answer.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango