2011-08-19 99 views
1

我正在使用IQueryable<T>接口。如何将以下SQL转换为Linq?

如何将以下sql语句翻译为IQueryable

select * from customer 
where joindate > DateTime.Now and 
     (customertype = 'system' or customerstatus = 'active') and 
     customerlocation = 'europe' 

回答

3

事情是这样的:

var result = from record in context.customer 
    where record.joindate > DateTime.Now && 
     (record.customertype == "system" || record.customerstatus == "active") && 
     record.customerlocation == "europe" 
    select record 

有一个很好的工具,Linqer,它可以帮助你的SQL查询转换为LINQ。当然,对于这种简单的情况来说,这太过于夸张,但如果你更熟悉SQL,那么你可以考虑它的重要查询。

你可以在这里找到它LINQER

1
var query = 
from i in db.customer 
where i.joindate > DateTime.Now 
&& (i.customertype == 'system' || i.customerstatus == 'active') 
&& i.customerlocation == 'europe' 
select i; 
0
var now = DateTime.Now; 
var queryable = Customers.Where(x=>x.joindate > now && (x.customertype == "system" || x.customerstatus == "active") && x.customerlocation == "europe") 

我不记得,如果LINQ将评估DateTime.Now所以我只是把它扔到提前时间的变量。

0

我pefer以下语法,但你可以使用查询语法,以及:

var results = yourContext.Customers.Where(c => (c.JoinDate > DateTime.Now) && 
    ((c.CustomerType.Equals("system") || (c.CustomerType.Equals("active")) && 
    (c.CustomerLocation.Equals("europe"))); 

使用查询语法:

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; 
0
var result = (from c in customer 
       where (c.joindate > DateTime.Now) && 
        (c.customertype == "system" || c.customerstatus == "active") && 
        (c.customerlocation == "europe") 
       select c) 
      .ToList(); 
+0

你不把customerlocation == '欧洲' – Gandarez