How to Resolve Error "CS0745: Expected contextual keyword 'by'" in C#
The Compiler Error CS0745 is a syntax error specific to LINQ Query Syntax. The message reads: "Expected contextual keyword 'by'".
In a LINQ query, the group clause requires two parts:
- What you want to group (the element).
- How you want to group it (the key).
The syntax must strictly follow the pattern: group [element] by [key]. This error occurs if you start a group clause but fail to provide the by keyword immediately after the element you are grouping.
Understanding LINQ Grouping Syntax
Grouping splits a sequence into buckets (groups) based on a key value. The compiler needs to know exactly which property or calculation acts as that key.
Correct Syntax:
var query = from item in collection
group item by item.Category; // ✅ 'by' is mandatory
If you simply write group item, the compiler stops and raises CS0745 because the statement is incomplete.
Scenario 1: Incomplete Group Clause
This is the most common cause. You define which variable to group, but you forget to specify the criteria.
Example of error
In this example, we try to group numbers, but we stop typing after the variable n.
using System.Linq;
using System.Collections.Generic;
public class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
// ⛔️ Error CS0745: Expected contextual keyword 'by'
// The compiler sees 'group n', but misses the logic for how to group them.
var query = from n in numbers
group n;
}
}
Solution
Add the by keyword followed by the expression defining the group key (e.g., grouping by even/odd status).
using System.Linq;
using System.Collections.Generic;
public class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
// ✅ Correct: Group numbers by whether they are Even (0) or Odd (1)
var query = from n in numbers
group n by n % 2;
foreach (var group in query)
{
System.Console.WriteLine($"Key: {group.Key}, Count: {group.Count()}");
}
}
}
Scenario 2: Misplaced 'into' Keyword
Sometimes developers mix up the order of operations when trying to continue a query after grouping (using into). The into keyword allows you to perform further operations on the results of the group, but it must come after the by clause is fully defined.
Example of error
Trying to store the result into a variable before defining the by criteria.
using System.Linq;
public class User { public string Role; }
public class Program
{
static void Main()
{
User[] users = { new User { Role = "Admin" } };
// ⛔️ Error CS0745: Expected contextual keyword 'by'
// The syntax 'group ... into' is invalid. It must be 'group ... by ... into'.
var query = from u in users
group u into g
select g;
}
}
Solution
Ensure the group [item] by [key] clause is complete before adding into [variable].
using System.Linq;
public class Program
{
static void Main()
{
User[] users = { new User { Role = "Admin" } };
// ✅ Correct: "group u BY u.Role", and then put that result INTO g.
var query = from u in users
group u by u.Role into g
select new { Role = g.Key, Count = g.Count() };
}
}
Conclusion
CS0745 indicates a broken LINQ sentence.
- Check the Clause: Locate the
groupkeyword. - Verify Structure: Ensure it is immediately followed by the variable name, and then immediately followed by the keyword
by. - Define the Key: Provide the property or calculation you are grouping by.