具有多个条件的Linq查询

时间:2012-02-29 21:56:47

标签: linq entity-framework

如何使用这些条件编写linq查询..首先我们必须检查OrderType,如果这是真的那么应该检查条件。如何编写查询..如果我关闭条件.OrderType.XYZ)然后它说'zj'不属于当前的上下文..如果我们删除没有错误但我们没有得到req结果

bool btnvisible= datacontext.GetOrders(new List<Items> { selectedItem }).
   .Where((zj => wo.OrderId== (int)BL.OrderType.PQR || zj.OrderId== (int)BL.OrderType.XYZ)
           &&( zj.OrderId== (int)BL.Statuses.Assigned
             || zj.OrderId== (int)BL.Statuses.Planned 
             || zj.OrderId== (int)BL.Statuses.InProgess
             || zj.OrderId== (int)BL.Statuses.Paused 
             || zj.OrderId== (int)BL.Statuses.Ready)).Any();
return btnEnable; 

3 个答案:

答案 0 :(得分:2)

我认为这是由于一些错位的括号。你有.Where((zj => ...) ... zj ...)。变量zj不存在于第一组括号之外。它应该是这样的:

bool btnvisible= datacontext.GetOrders(new List<Items> { selectedItem })
   .Where(zj => (wo.OrderId== (int)BL.OrderType.PQR || zj.OrderId== (int)BL.OrderType.XYZ)
           && (zj.OrderId== (int)BL.Statuses.Assigned
             || zj.OrderId== (int)BL.Statuses.Planned 
             || zj.OrderId== (int)BL.Statuses.InProgess
             || zj.OrderId== (int)BL.Statuses.Paused 
             || zj.OrderId== (int)BL.Statuses.Ready)).Any();
return btnEnable; 

答案 1 :(得分:2)

var readyStats = new [] {
   (int)BL.Statuses.Assigned,
   (int)BL.Statuses.Planned,
   (int)BL.Statuses.InProgess,
   (int)BL.Statuses.Paused,
   (int)BL.Statuses.Ready,
};

var orderTypes = new [] {
    (int)BL.OrderType.PQR,
    (int)BL.OrderType.XYZ
}

bool btnvisible= datacontext.GetOrders(new List<Items> { selectedItem }).
   .Where(wo => orderTypes.Contains(wo.OrderId) && readyStats.Contains(wo.OrderId)).Any();

bool btnvisible= datacontext.GetOrders(new List<Items> { selectedItem }).
       .Any(wo => orderTypes.Contains(wo.OrderId) && readyStats.Contains(wo.OrderId));

我认为问题在于:

.Where((zj => wo.

正确的版本应该是:

   .Where(zj => zj.

答案 2 :(得分:2)

您似乎有一个不匹配的括号问题。

.Where((zj => wo.OrderId== (int)BL.OrderType.PQR || zj.OrderId== (int)BL.OrderType.XYZ) 
       ^                                                                              ^

这与变量的范围有关。重新审视它。