简体   繁体   English

LINQ中的where子句-C#

[英]Where clause in LINQ - C#

I have the following which works in SQL Query Analyzer . 我有以下在SQL查询分析器中有效的方法

select oh.*
from order_history oh
join orders o on o.order_id = oh.order_id
where oh.order_id = 20119 and oh.date_inserted = (
    select max(date_inserted) from order_history where order_id = oh.order_id
    group by order_id
)

How would I go about converting to LINQ ? 我将如何转换为LINQ From test code, the compiler complained: 从测试代码,编译器抱怨:

Error Operator '&&' cannot be applied to operands of type 'int' and 'System.DateTime' 错误运算符“ &&”不能应用于类型为“ int”和“ System.DateTime”的操作数

My LINQ code: 我的LINQ代码:

var query = from ch in cdo.order_histories
    join c in cdo.orders on ch.order_id equals c.order_id
    where (ch.order_id.equals(1234)) &&
    (ch.date_inserted == (cdo.order_histories.Max(p => p.date_inserted)))
    select new OrderData() { };

Update: I was not using '==' for comparing. 更新:我没有使用'=='进行比较。

Item remaining is this from my SQL query: 剩余的项目是从我的SQL查询中获得的:

oh.date_inserted = (
select max(date_inserted) from order_history where order_id = oh.order_id
group by order_id)

How do I do this in LINQ? 如何在LINQ中做到这一点?

It look like you are missing an equals sign somewhere when filtering on the order_id field. 似乎在对order_id字段进行过滤时,您在某处缺少等号。 You probably have: 您可能有:

oh.order_id = 20119 && ...

Whereas you should have: 而您应该具有:

oh.order_id == 20119 && ...

Note the equality operator vs. the assignment operator. 注意相等运算符与赋值运算符。 The result of an assignment operator is the value that was assigned, which is why your error says you can't compare operands of int and System.DateTime. 赋值运算符的结果是已赋值,这就是为什么您的错误提示您无法比较int和System.DateTime的操作数的原因。

I also assume you have the same problem on the check against the value of date_inserted as well. 我还假设您在检查date_inserted的值时也date_inserted相同的问题。

For the second part of your question, you are close in the conversion of the correlated sub query. 对于问题的第二部分,您正在完成相关子查询的转换。

In SQL you have: 在SQL中,您具有:

oh.date_inserted = (
select max(date_inserted) from order_history where order_id = oh.order_id 
group by order_id)

And in LINQ-to-SQL you have 在LINQ-to-SQL中

ch.date_inserted == (cdo.order_histories.Max(p => p.date_inserted))

You just have to add the filter for the order_histories which takes advantage of closures to capture the order_id value on the ch instance like so: 您只需要为order_histories添加过滤器, order_histories利用闭包来捕获ch实例上的order_id值,如下所示:

ch.date_inserted == (cdo.order_histories.
    Where(ch2 => ch2.order_id == ch.order_id).
    Max(p => p.date_inserted))

You could translate the SQL into LINQ... or you could write the LINQ for what you want. 您可以将SQL转换为LINQ ...,也可以根据需要编写LINQ。

var result = cdo.order_histories
  .Where(oh => oh.order_id == 20119)
  .OrderByDescending(oh => oh.date_inserted)
  .Take(1)
  .Select(oh => new {history = oh, order = oh.order}
  .Single();

Agreed, some C# code is needed here, but off the top of my head- you're using "==" (evaluation) rather than "=" (assignment), correct? 同意,这里需要一些C#代码,但是在我的头上-您使用的是“ ==”(评估)而不是“ =”(赋值),对吗? C# makes a distinction here where SQL does not. C#在这里区分出SQL没有的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM