如何在 linq to sql 中编写不等于运算符?

How to write not equal operator in linq to sql?(如何在 linq to sql 中编写不等于运算符?)

本文介绍了如何在 linq to sql 中编写不等于运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using (RapidWorkflowDataContext context = new RapidWorkflowDataContext()){var query = from w in context.WorkflowInstances从 c 在 context.Workflows其中 EmpWorkflowIDs.Contains((int)w.ID) &&w.CurrentStateID != c.LastStateID选择 w;返回 query.ToList();}

我有 2 个表:Workflows 和 WorkflowInstances.

用于存储对象的工作流和用于存储实例的工作流实例.

工作流表:ID、名称、FirstStateID、LastStateID

WorkflowInstances 表:ID,Name,WorkflowID,CurrentStateID

如何在 linq to sql 中编写查询以从 WorkflowInstances 中选择 CurrentStateID 不等于 LastStateID 的实例

解决方案

你需要修改join在2个表之间的相关列上,然后你在where子句中添加你的条件,如下所示:

using (RapidWorkflowDataContext context = new RapidWorkflowDataContext()){var query = from w in context.WorkflowInstances在 w.WorkflowID 上的 context.Workflows 中加入 c 等于 c.ID其中 EmpWorkflowIDs.Contains((int)w.ID)&&w.CurrentStateID != c.LastStateID选择 w;返回 query.ToList();}

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();
                    }

I have 2 tables: Workflows and WorkflowInstances.

Workflows to store objects and workflowInstances to store instances.

Workflows Table: ID,Name,FirstStateID,LastStateID

WorkflowInstances Table: ID,Name,WorkflowID,CurrentStateID

How to write a query in linq to sql to select the instances from WorkflowInstances which CurrentStateID not equal LastStateID

解决方案

You need to revise the join to be on the related columns between the 2 tables, then you add your condition in the where clause, like the following:

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();
                        }

这篇关于如何在 linq to sql 中编写不等于运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 linq to sql 中编写不等于运算符?

基础教程推荐