How to Resolve Error "CS0743: Expected contextual keyword 'on'" in C#
The Compiler Error CS0743 is a syntax error specific to LINQ Query Expressions. The message reads: "Expected contextual keyword 'on'".
In C#, the join clause in LINQ has a very strict grammatical structure: join variable in collection on leftKey equals rightKey. This error occurs when the compiler successfully parses the join variable in collection part, but fails to find the required on keyword immediately afterwards. This usually happens due to typos, using the wrong keyword (like where), or placing the condition in the wrong order.
This guide explains the correct anatomy of a LINQ join and how to fix malformed queries.
Understanding the LINQ join Syntax
The structure of a join clause corresponds roughly to SQL, but the order is enforced for type inference:
join: Starts the clause.variable: The name you give to the item from the second list.in: Separator.collection: The second data source.on: (Required) signals the start of the key comparison.leftKeyequalsrightKey: The matching logic.
If step 5 is missing or replaced by something else, CS0743 is raised.
Scenario 1: Missing the on Keyword
This is a simple syntax error where the developer forgets the keyword or accidentally deletes it while editing.
Example of error
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 CS0743: Expected contextual keyword 'on'.
// The compiler sees 'join o in orders', then immediately sees 'c.Id'.
// It doesn't know how to connect them.
var query = from c in customers
join o in orders c.Id equals o.CustomerId
select new { c, o };
}
}
Solution: Insert 'on'
Add the keyword between the collection name and the first key.
public class Program
{
static void Main()
{
var customers = new List<Customer>();
var orders = new List<Order>();
// ✅ Correct: Added 'on' to define the relationship.
var query = from c in customers
join o in orders on c.Id equals o.CustomerId
select new { c, o };
}
}
Scenario 2: Using where Instead of on
Developers familiar with SQL (where JOIN ... ON and JOIN ... WHERE logic sometimes blurs) or just standard C# filtering often mistakenly try to use where or if inside the join clause.
Example of error
Using where to define the join condition.
public void JoinData(List<Customer> customers, List<Order> orders)
{
// ⛔️ Error CS0743: Expected 'on', found 'where'.
var query = from c in customers
join o in orders where c.Id == o.CustomerId
select o;
}
Solution: Use on ... equals
LINQ joins strictly require the on keyword followed by the equals keyword. You cannot use == or where to define the correlation.
public void JoinData(List<Customer> customers, List<Order> orders)
{
// ✅ Correct: Use 'on' and 'equals'
var query = from c in customers
join o in orders on c.Id equals o.CustomerId
select o;
}
If you need to filter results further (e.g., where o.Price > 100), place a standard where clause after the join line is fully completed.
Scenario 3: Incorrect Word Order
Sometimes rewriting a query leads to keywords getting shuffled. If the on keyword appears before the collection is defined, the syntax is invalid.
Example of error
Placing the condition before the collection.
public void Process(List<Customer> customers, List<Order> orders)
{
// ⛔️ Error CS0743: The syntax structure is mixed up.
// C# expects 'in [Collection] on', not 'on [Keys] in'.
var query = from c in customers
join o on c.Id equals o.CustomerId in orders
select o;
}
Solution: Fix the Order
Ensure the order is: join [Var] in [Collection] on ...
public void Process(List<Customer> customers, List<Order> orders)
{
// ✅ Correct: Order restored
var query = from c in customers
join o in orders on c.Id equals o.CustomerId
select o;
}
Conclusion
CS0743 is the compiler enforcing the LINQ grammar.
- Check the Join: Locate the
joinline in your query. - Verify Structure: Ensure it follows the pattern:
join ... in ... on ... equals .... - Replace Keywords: If you used
whereor==, replace them withonandequals.