C# Error CS1941 – The type of one of the expressions in the {0} clause is incorrect. Type inference failed in the call to ‘{1}’.

C# Error

CS1941 – The type of one of the expressions in the {0} clause is incorrect. Type inference failed in the call to ‘{1}’.

Reason for the Error & Solution

The type of one of the expressions in the ‘clause’ clause is incorrect. Type inference failed in the call to ‘method’.

Type inference in query expressions flows from the type of the elements in the data source(s).

To correct this error

  1. If it is not immediately obvious why the error is occurring, examine the query carefully and trace the type of the result of each clause from the data source to the point where the error is occurring.

Example

The following code generates CS1941 because the equals operator is being asked to compare an int to a string.

// cs1941.cs  
using System.Collections;  
using System.Linq;  
class Test  
{  
    static int Main()  
    {  
        var nums = new[] { 1, 2, 3, 4, 5, 6 };  
        var words = new string[] { "lake", "mountain", "sky" };  
        IEnumerable e = from n in nums  
                        join w in words on n equals w // CS1941  
                        select w;  
        return 0;  
    }  
}  

The method in which type inference fails is the method that the query clause is translated to at compile time.

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...