如何在linq中写入不等于sql的运算符?

时间:2013-08-16 13:11:28

标签: c# linq linq-to-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,名称,FirstStateID,LastStateID

WorkflowInstances表:ID,名称,WorkflowID,CurrentStateID

如何在linq中将查询写入sql以从WorkflowInstances中选择实例,其中CurrentStateID不等于LastStateID

3 个答案:

答案 0 :(得分:5)

您需要将连接修改为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();
                        }

答案 1 :(得分:4)

如果您正在使用lambda表达式,那么not(!)会去那里:

.Where(p => !p.Whatever...)

答案 2 :(得分:0)

您可以删除join,它应该类似于:

var query = from w in context.WorkflowInstances
            where !context.Workflows.Any(c => w.CurrentStateID != c.LastStateID) && EmpWorkflowIDs.Contains((int)w.ID)
            select w;