Group by standard DB Function

Posts   
 
    
Freddy V
User
Posts: 11
Joined: 05-Feb-2009
# Posted on: 18-Aug-2009 17:35:33   

I'm trying to write a group by query, that groups on the date part of a datetime field. According to (http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/Linq/gencode_linq_functionmappings.htm) the Date property of the datetime type should be 'usable in all areas of a linq query'.

But when I execute the code below I get this exception Unable to cast object of type 'SD.LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.DbFunctionCallExpression' to type 'SD.LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.EntityFieldExpression'.

var results = (from UserSessionRecord in metaData.UserSession
group UserSessionRecord by UserSessionRecord.SessionDate.Date into g
select g);
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 18-Aug-2009 22:11:32   

Hi

Please can you confirm the version you are using and other relevant into, as described here

If you are not currently using the latest version please can you download and try that as fixes are being made to the Linq provider all the time...

Thanks

Matt

Freddy V
User
Posts: 11
Joined: 05-Feb-2009
# Posted on: 19-Aug-2009 09:36:20   

I just Dl'ed and copied build 08112009 of the RTL's, and cheched if the new dlls were also copied to the bin dir of my app.

But alas, the same errror.

I'm Using C#, .Net 3.5, SelfServicing, TwoClasses2008

Stacktrace:

[InvalidCastException: Unable to cast object of type 'SD.LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.DbFunctionCallExpression' to type 'SD.LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.EntityFieldExpression'.]
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.CoerceGroupByToFullQueryForProjection(GroupByExpression referencedGroupBy, Type groupingType, Type keyType) +569
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleSetReferenceExpression(SetReferenceExpression expressionToHandle) +1098
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle) +1181
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle) +187
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleProjectionExpression(ProjectionExpression expressionToHandle) +150
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle) +1058
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle) +187
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleSelectExpression(SelectExpression expressionToHandle, SelectExpression newInstance) +65
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleSelectExpression(SelectExpression expressionToHandle) +75
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleSelectExpression(SelectExpression expressionToHandle) +103
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle) +1141
   SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle) +187
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.HandleExpressionTree(Expression expression) +750
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression) +13
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression) +17
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute() +16
   SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +16
   ProjectTracker.Web.Admin.rapportering.bindData() in D:\Projects\ADC\ProjectTracker\Web\Admin\rapportering.aspx.cs:27
   ProjectTracker.Web.Admin.rapportering.Page_Load(Object sender, EventArgs e) in D:\Projects\ADC\ProjectTracker\Web\Admin\rapportering.aspx.cs:12
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 19-Aug-2009 10:08:25   

Please try the following:

            var q = from o in metaData.Orders
                    group o by new{o.OrderDate.Date} into g
                    select new { key = g.Key, count = g.Count()}; 

If the dateTime filed is nullable, then try this:

            var q = from o in metaData.Orders
                    group o by new{o.OrderDate.Value.Date} into g
                    select new { key = g.Key, count = g.Count()}; 
Freddy V
User
Posts: 11
Joined: 05-Feb-2009
# Posted on: 19-Aug-2009 11:32:35   

Thanks for the advice. But the field isn't nullable, so there's no Value property. I would actually think that a not nullable field would make things easier confused

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 19-Aug-2009 11:38:33   

Sure so the first piece of code I've posted should work for you.

Freddy V
User
Posts: 11
Joined: 05-Feb-2009
# Posted on: 19-Aug-2009 12:53:47   

Walaa wrote:

Sure so the first piece of code I've posted should work for you.

Sorry Walaa, I'm a bad reader flushed The solution works.