How to Resolve Error "CS0744: Expected contextual keyword 'equals'" in C#
The Compiler Error CS0744 is a syntax error specific to LINQ Query Expressions. The message reads: "Expected contextual keyword 'equals'".
In C#, the join clause is optimized for "Equi-Joins" (matching elements based on equality). The syntax is strictly defined as on key1 equals key2. The error occurs when you use standard C# comparison operators like ==, =, or != instead of the required contextual keyword equals. The compiler does not accept boolean expressions (like a.ID == b.ID) inside the on clause of a join; it specifically demands the equals keyword to separate the two keys being compared.
This guide explains the correct syntax for LINQ joins and how to fix common operator mistakes.
Understanding the join Syntax
The structure of a LINQ join is not a generic conditional statement (like an if or where). It is a mapping instruction.
Correct Syntax:
join [var] in [collection] on [LeftKey] equals [RightKey]
- LeftKey: A property from the outer sequence.
- RightKey: A property from the inner (joined) sequence.
- equals: The specific separator that tells the compiler these two keys must match.
Scenario 1: Using the == Operator
This is the most frequent cause. Developers are accustomed to using == for comparisons in C#. However, inside a join ... on ... clause, == is syntactically invalid.
Example of error
Attempting to join two lists using the double-equals operator.
using System.Linq;
using System.Collections.Generic;
public class Customer { public int Id; }
public class Order { public int CustomerId; }
public class Program
{
static void Main()
{
var customers = new List<Customer>();
var orders = new List<Order>();
// ⛔️ Error CS0744: Expected contextual keyword 'equals'.
// The compiler sees 'on c.Id', and expects 'equals' next.
// It found '==' instead.
var query = from c in customers
join o in orders on c.Id == o.CustomerId
select o;
}
}
Solution: Replace == with equals
Simply change the operator to the keyword. Note that equals allows the compiler to perform efficient hash-based lookups, whereas == implies an arbitrary boolean check.
public class Program
{
static void Main()
{
var customers = new List<Customer>();
var orders = new List<Order>();
// ✅ Correct: Used the 'equals' keyword.
var query = from c in customers
join o in orders on c.Id equals o.CustomerId
select o;
}
}
Non-Equi Joins: If you want to join based on logic like "greater than" (>) or "not equal" (!=), you cannot use the join keyword. You must use a cross-join (multiple from clauses) followed by a standard where clause.
Scenario 2: Using .Equals() or Expressions
Sometimes developers try to write the join condition as a method call or a boolean expression, treating the on clause like a where clause.
Example of error
Using the .Equals() method inside the join syntax.
public void JoinData(List<string> left, List<string> right)
{
// ⛔️ Error CS0744: The syntax expects 'KeyA equals KeyB'.
// Here, 'a.Equals(b)' is a single boolean expression, which is invalid syntax for 'on'.
var query = from a in left
join b in right on a.Equals(b)
select a;
}
Solution: Use equals Keyword
You must separate the two sides. The equals keyword implicitly handles the equality check (using the type's default equality comparer).
public void JoinData(List<string> left, List<string> right)
{
// ✅ Correct: 'a' is the left key, 'b' is the right key.
var query = from a in left
join b in right on a equals b
select a;
}
Conclusion
CS0744 is a strict syntax enforcement for LINQ queries.
- Check the Operator: Look between your keys in the
joinline. - Replace: Change
==,=, or!=toequals. - Check Structure: Ensure the syntax follows
on [Left] equals [Right].