I am using IQueryable<T>
interface.
How can I translate the follow开发者_如何学运维ing sql statement to IQueryable
?
select * from customer
where joindate > DateTime.Now and
(customertype = 'system' or customerstatus = 'active') and
customerlocation = 'europe'
Something like this :
var result = from record in context.customer
where record.joindate > DateTime.Now &&
(record.customertype == "system" || record.customerstatus == "active") &&
record.customerlocation == "europe"
select record
There is a nice tool, Linqer, which can help you to convert SQL queries to LINQ. Of course for such simple cases it is overkill, but you can consider it for heavy queries of course if you are more familiar with SQL.
You can find it here LINQER.
var query =
from i in db.customer
where i.joindate > DateTime.Now
&& (i.customertype == 'system' || i.customerstatus == 'active')
&& i.customerlocation == 'europe'
select i;
var now = DateTime.Now;
var queryable = Customers.Where(x=>x.joindate > now && (x.customertype == "system" || x.customerstatus == "active") && x.customerlocation == "europe")
I can't remember if linq will evaluate DateTime.Now so I just threw it into a variable ahead of time.
I pefer the following syntax but you could use query syntax as well:
var results = yourContext.Customers.Where(c => (c.JoinDate > DateTime.Now) &&
((c.CustomerType.Equals("system") || (c.CustomerType.Equals("active")) &&
(c.CustomerLocation.Equals("europe")));
Using query syntax:
var results = from c in yourContext.Customers
where (c.JoinDate > DateTime.Now) &&
(c.CustomerType.Equals("system") || c.CustomerStatus.Equals("active")) &&
c.CustomerLocation.Equals("europe")
select c;
var result = (from c in customer
where (c.joindate > DateTime.Now) &&
(c.customertype == "system" || c.customerstatus == "active") &&
(c.customerlocation == "europe")
select c)
.ToList();
精彩评论