How to write a non-equal operator in 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(); } 

I have 2 tables: Workflows and WorkflowInstances.

Workflows for storing objects and workflowInstances for storing instances.

Workflow table: identifier, name, FirstStateID, LastStateID

Workflow table: identifier, name, workflow, CurrentStateID

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

+6
source share
3 answers

You need to redefine the join to be in the appropriate columns between the two tables, then you add your condition to the where clause, as shown below:

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

If you use lambda expressions, then not(!) Goes there:

 .Where(p => !p.Whatever...) 
+3
source

You can eliminate join , and it should be something like:

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

Source: https://habr.com/ru/post/951882/


All Articles