I get another error now: InvalidOperationException - Code supposed to be unreachable
public class GroupByYear
{
public int Year { get; set; }
public int Month { get; set; }
public Object Group { get; set; }
public string GroupString { get; set; }
public int Count { get; set; }
public GroupByYear(int year, int month)
{
Year = year;
Month = month;
}
public GroupByYear(int year, int month, object group)
: this(year, month)
{
Group = group;
}
public GroupByYear(int year, int month, string groupString)
: this(year, month)
{
GroupString = groupString;
}
public GroupByYear()
{
}
}
void Main()
{
var ordersGroupedByYearNamed = Order.GroupBy(o => new GroupByYear(o.OrderDate.Value.Year,o.OrderDate.Value.Month){Group=o.ShipCity} );
ordersGroupedByYearNamed.Select(o => new GroupByYear(o.Key.Year, o.Key.Month) {Group = o.Key.Group}).ToList().Dump();
}
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.MemberAssignmentRewriter..ctor(MemberAssignment binding, StackSpiller spiller, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.BindingRewriter.Create(MemberBinding binding, StackSpiller spiller, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMemberInitExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteUnaryExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda)
at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.SetupProjectionElementsForExecution(QueryExpression toExecute)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.ExecuteValueListProjection(QueryExpression toExecute)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression)
at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
but if I add a cast to the last line
ordersGroupedByYearNamed.Select(o => new GroupByYear(o.Key.Year, o.Key.Month) {Group = (string)o.Key.Group}).ToList().Dump();
it works fine
It seems LINQ to NHibernate has a similar problem:
http://connect.microsoft.com/VisualStudio/feedback/details/557075/code-supposed-to-be-unreachable-exception-from-system-linq/