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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18274141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:46:07  来源:igfitidea点击:

How to write not equal operator in linq to sql?

c#linqlinq-to-sql

提问by Anas Salem

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.

我有 2 个表:工作流和工作流实例。

Workflows to store objects and workflowInstances to store instances.

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

Workflows Table: ID,Name,FirstStateID,LastStateID

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

WorkflowInstances Table: ID,Name,WorkflowID,CurrentStateID

工作流实例表:ID、名称、工作流ID、当前状态ID

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

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

采纳答案by Adel Khayata

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:

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

回答by Anatolii Gabuza

You can eliminate joinand it should be something like:

你可以消除join,它应该是这样的:

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

回答by renakre

If you are using lambda expressions, then not(!)goes there:

如果您使用的是 lambda 表达式,则not(!)转到那里:

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