如何将以下 SQL 转换为 Linq?

发布于 2024-11-30 08:46:28 字数 281 浏览 2 评论 0原文

我正在使用 IQueryable 接口。

如何将以下 sql 语句转换为 IQueryable

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

I am using IQueryable<T> interface.

How can I translate the following sql statement to IQueryable?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾忌 2024-12-07 08:46:28

像这样的事情:

    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

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.

天赋异禀 2024-12-07 08:46:28
var query = 
from i in db.customer
where i.joindate > DateTime.Now 
&& (i.customertype == 'system' || i.customerstatus == 'active')
&& i.customerlocation == 'europe'
select i;
var query = 
from i in db.customer
where i.joindate > DateTime.Now 
&& (i.customertype == 'system' || i.customerstatus == 'active')
&& i.customerlocation == 'europe'
select i;
二手情话 2024-12-07 08:46:28
var now = DateTime.Now;
var queryable = Customers.Where(x=>x.joindate > now && (x.customertype == "system" || x.customerstatus == "active") && x.customerlocation == "europe")

我不记得 linq 是否会计算 DateTime.Now 所以我只是提前将其放入变量中。

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.

心作怪 2024-12-07 08:46:28

我更喜欢以下语法,但您也可以使用查询语法:

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;  

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;  
桃气十足 2024-12-07 08:46:28
var result = (from c in customer
              where (c.joindate > DateTime.Now) &&
                    (c.customertype == "system" || c.customerstatus == "active") &&
                    (c.customerlocation == "europe")
              select c)
             .ToList();
var result = (from c in customer
              where (c.joindate > DateTime.Now) &&
                    (c.customertype == "system" || c.customerstatus == "active") &&
                    (c.customerlocation == "europe")
              select c)
             .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文