linqtelerik-open-access

Linq to Group By


I have a function say

GetGroupByData(DateTime p1,DateTime p2) 
{
    var data = dbContex.tableName
                       .Where(someconditions)
                       .GroupBy(x=>SqlScalarFunction(x.DateStart,p1));
}

     CREATE FUNCTION dbo.SqlScalarFunction(@Date DateTime, @DateFirst DateTime) 
     RETURNS Int 
     AS 
       BEGIN 
        RETURN Cast((cast(@Date - @DateFirst as float) / 7.0) as int) + 1
         END

I am getting an error:

Column 'tableName.DateStart' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Statement(s) could not be prepared.

How can I achieve the desire grouping?


Solution

  • I solved the issue as

    GetGroupByData(DateTime p1,DateTime p2) 
    {
     var function = "dbo.SqlScalarFunction({0}, '" + p1.ToString("yyyy-MM-dd HH:mm:ss") + "')";
       var data = dbContex.tableName.Where(someconditions)
       .GroupBy(x => function.SQL<Int32>(x.DateStartUTC));
    }
    

    I found the solution here :http://www.telerik.com/forums/converting-to-datetime-in-where-clause