Tuesday, 18 September 2007

While documenting/testing Genome 3.3 I stumbled about this strange behaviour, which seems to be a bug of the C# 3.0 beta 2 compiler.

I was trying to compile the following GROUP BY example with Genome:

var ordersPerCountryPerYear1 = from o in Helper.DB.Extent()
                               group o by new { o.Customer.Address.Country, o.OrderDate.Value.Year } into g
                               select new
                               {
                                 Country = g.Key.Country,
                                 Year = g.Key.Year,
                                 OrderCount = g.Count()
                               };

And received the following error from the compiler:

error CS1061: 'System.Linq.IGrouping' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?)

However, my team insisted that the extension method Count() is provided by Genome. To find out, why the compiler does not find it, they asked me to call it directly in Main():

TechTalk.Genome.Extensions.Linq.InternalIGroupingExtensions.Count(null);

After inserting this call in my code, the program suddenly compiled (including the statement, the C# compiler complained about previously).

We think this is a bug of the compiler. As a workaround I now have the following method on one class in my project to satisfy the compiler :-) :

static void ThisIsNeverCalled()
{
  TechTalk.Genome.Extensions.Linq.InternalIGroupingExtensions.Count(null);
}

Posted by Chris

Technorati Tags:

Tuesday, 18 September 2007 17:24:01 (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [5]  |  Related posts:
Reusing query logic in LINQ
Genome vs. .NET 3.5 SP1 (Entity Framework)
Misusing c# lambda expressions
LINQ and object relational mapping

Tuesday, 18 September 2007 23:55:15 (W. Europe Daylight Time, UTC+02:00)
Hi Chris,
Have you imported the namespace containing the extension methods before using it. Something like "using TechTalk.Genome.Extensions.Linq.InternalIGroupingExtensions;" before the Query should do the trick.

Let me know if this does not work for you ....
Tuesday, 18 September 2007 23:55:43 (W. Europe Daylight Time, UTC+02:00)
Hi Chris,
Have you imported the namespace containing the extension methods before using it. Something like "using TechTalk.Genome.Extensions.Linq.InternalIGroupingExtensions;" before the Query should do the trick.
Let me know if this does not work for you ....
Wednesday, 19 September 2007 00:17:00 (W. Europe Daylight Time, UTC+02:00)
I was not abe to repro the bug that you suggested ..
wrote an extension like below, and compiled it to a dll
namespace Extensions
{
namespace Nest1
{

public static class Extensions
{
public static int Add(this int i, int j) { return i + j; }
}
}
}
wrote client that links to the dll like
namespace TestExtensions
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int j = 10;
i.Add(j);
}

static void Do()
{
Extensions.Nest1.Extensions.Add( 10, 20);
}
}
}
and the code would not compile ...

importing the namespace make everthing work fin, like ...
namespace TestExtensions
{
using Extensions.Nest1;

class Program
{
static void Main(string[] args)
{
int i = 0;
int j = 10;
i.Add(j);
}

static void Do()
{
Extensions.Nest1.Extensions.Add( 10, 20);
}
}
}

Let me if you can send me the project that is causing the problem i can see if this is a real bug ...

Thanks Sree
Wednesday, 19 September 2007 08:43:32 (W. Europe Daylight Time, UTC+02:00)
Hi Sree,

The namespace using was included.

We also did not experience this problem with other extension methods. I have tried to put together a simplified repro, but it works there. There must be some speciality of that project...

We will send the original one for you to investigate.

Thx,
Gaspar
Tuesday, 25 September 2007 10:01:10 (W. Europe Daylight Time, UTC+02:00)
With the great help of Sree it turned out that the problem was in the InternalIGroupingExtensions class, that was not attributed well to act as an extension method class. We had to do the manual annotation (as well) because of the build process running in .NET 2.0.

Generally you cannot run into such problems if you compile the extension method with the C# 3.0 compiler.
Comments are closed.