如何在linq中写入不等于sql的运算符?
using (RapidWorkflowDataContext context = new RapidWorkflowDataContext()) { var query = from w in context.WorkflowInstances from c in context.Workflows where EmpWorkflowIDs.Contains((int)w.ID) && w.CurrentStateID != c.LastStateID select w; return query.ToList(); }
我有2个表:工作流和WorkflowInstances。
用于存储对象的工作流和用于存储实例的workflowInstances。
工作流表:ID,Name,FirstStateID,LastStateID
WorkflowInstances表:ID,Name,WorkflowID,CurrentStateID
如何在linq中编写查询到sql,从WorkflowInstances中选择实例,其中CurrentStateID不等于LastStateID
您需要将连接修改为在2个表之间的相关列上,然后在where子句中添加条件,如下所示:
using (RapidWorkflowDataContext context = new RapidWorkflowDataContext()) { var query = from w in context.WorkflowInstances join c in context.Workflows on w.WorkflowID equals c.ID where EmpWorkflowIDs.Contains((int)w.ID) && w.CurrentStateID != c.LastStateID select w; return query.ToList(); }
如果你正在使用lambda表达式,那么not(!)
去那里:
.Where(p => !p.Whatever...)
你可以消除join
,它应该是这样的:
var query = from w in context.WorkflowInstances where !context.Workflows.Any(c => w.CurrentStateID != c.LastStateID) && EmpWorkflowIDs.Contains((int)w.ID) select w;