Linq lambda查询中的OR逻辑帮助 [英] Help with OR logic in Linq lambda query

查看:1413
本文介绍了Linq lambda查询中的OR逻辑帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的linq查询上我有点卡住了.我正在尝试查找状态为已完成"以外的所有订单,或者上个月内已完成的订单.

I'm a bit stuck on the following linq query. I'm trying to find all orders which have a state other than "Completed", or which have been completed within the last month.

_ordersRepository.GetAllByFilter(
     o => o.OrderStatus
         .OrderByDescending(os => os.StatusUpdate.Date)
         .First() 

          // so at this point I have found the most recent status,
          // and I need to do something like...

          /* pseudo code */
          WHERE
              StatusUpdate.StatusType.Name != "Completed" 
          OR
              StatusUpdate.Date > DateTime.Today.AddMonths(-1)
    );

编辑进行澄清: 我遇到的困难是我没有直接的方法来获取诸如Order.Status之类的当前状态.状态一直保存在StatusUpdate表中. StatusUpdate通过OrderStatus链接器表连接到Order(一个订单具有许多StatusUpdates).因此,如果您查看上面的尝试代码,就会发现我正在通过以下方式找出当前状态:

EDIT to clarify: The difficulty I'm having is that I don't have a direct way to get the current status like Order.Status. The status is kept historically in a StatusUpdate table. A StatusUpdate is connected to an Order by an OrderStatus linker table (one Order has many StatusUpdates). So if you look at my attempted code above, you see I am finding out the current status with:

Order.OrderStatus.OrderByDescending(os => os.StatusUpdate.Date).First()

这给了我StatusUpdate,它对应于当前订单状态.我现在需要检查:

This gives me a StatusUpdate corresponding to the current order status. I now need to check:

StatusUpdate.StatusType.Name == "Completed" // StatusType is like an enum lookup table
OR
StatusUpdate.Date > lastMonth 

我几乎可以知道我该怎么做,但是并非没有效率低下的问题.

I can almost get how I could do it, but not without being horribly inefficient.

推荐答案

我想我现在明白了.就像我在应用程序中执行此操作一样,我具有状态,订单和运动"表,其中最后一个是当前表.更新我的示例.

I think I got it now. It's like I do it in my apps, I have Statuses, Orders and "Movements" table where the last one is the current. Updating my example.

DateTime dateToMatch = DateTime.Today.AddMonths(-1);
var orders = from c in Context.Orders
let currentMovement == c.Movements.OrderByDescending(x => x.PerformedOn).FirstOrDefault()
let currentStatusName == currentMovement.Status.Name
where currentStatusName != "Completed" || (currentStatusName == "Completed" && currentMovement.PerformedOn > dateToMatch)
select c;

这篇关于Linq lambda查询中的OR逻辑帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆