C# Error CS0745 – Expected contextual keyword ‘by’

C# Error

CS0745 – Expected contextual keyword ‘by’

Reason for the Error & Solution

Expected contextual keyword ‘by’

The pattern for the group clause is group...by followed by an optional into, as shown in the following example:

string[] names = { "Bob", "Bill", "Jonetta", "Mary" };  
  
var query = from name in names  
            group name by name[0];  

or

var query2 = from name in names  
             group name by name[0] into g  
             //...additional query clauses  

To correct this error

  1. Add the by keyword to the group clause.

Example

The following code generates CS0745:

// cs0745.cs  
using System;  
using System.Linq;  
  
public class C  
{  
    public static int Main()  
    {  
        string[] names = { "Bob", "Bill", "Jonetta", "Mary" };  
  
        var query = from name in names  
                    group name name[0]; // CS0745  
  
        return 1;  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...