ToDataSourceResult Problems with NHibernate in a Grid

6 posts, 0 answers
  1. Frank
    Frank avatar
    1 posts
    Member since:
    Dec 2012

    Posted 24 Feb 2014 Link to this post

    Hello,

    I am having trouble using the ToDataSourceResult method in combination with NHibernate.

    Here is a minimal example of what I'm doing:

                using ( var session = NHibernate.DefaultFactory.OpenStatelessSession())
                {
                    IQueryable<MyDataRow> myQuery = session.Query<MyDataRow>();
                    DataSourceResult myResult = dataRowRepo.ToDataSourceResult(request, x => new ListRowViewModel()
                    {
                        ID = x.ID,
                        Property2 = x.Property2
                    });
                    return Json(myResult, JsonRequestBehavior.AllowGet);
                }

    This works perfectly fine as long as I don't try to use aggregate or grouping. As soon as I start that my service returns something like this:

    Specified method is not supported.

    Or for aggregation:
    Query Source could not be identified: ItemName = x,
    ItemType = MyDataRow, Expression
    = from MyDataRow x in
    value(NHibernate.Linq.NhQueryable`1[MyDataRow])

    And help or suggestions would be greatly appreciated.
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 26 Feb 2014 Link to this post

    Hi Frank,

    It seems that the NHibernate Linq provider can't interpret some of the LINQ expression trees that ToDataSourceResult generates. In such cases we can suggest only two things - implement custom binding thus eliminating the ToDataSourceResult call or try to perform the problematic operations (e.g. grouping) in memory. Here is how to do that:

            public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request)
            {
                var customers = GetCustomers();

                var grouping = request.Groups;

                //Clear the grouping options
                request.Groups = null;

                //Execute the query without grouping
                var result = customers.ToDataSourceResult(request);

                //Restore grouping
                request.Groups = grouping;

                //Call ToDataSourceResult to do the grouping in memory
                result = result.Data.ToDataSourceResult(request);

                return Json(result);
            }

    Regards,
    Atanas Korchev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Mike
    Mike avatar
    75 posts
    Member since:
    Nov 2011

    Posted 26 May 2014 in reply to Atanas Korchev Link to this post

    Hello,

    I'm using the latest version of Kendo UI, and still have the same problem as you Frank.
    Is there any news about this subject ? 

    Thanks for help
    Regards
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 28 May 2014 Link to this post

    Hi,

    The code which handles grouping hasn't been changed. Perhaps the NHibernate Linq support has been improved. If this is still a problem with the latest version of NHibernate you can try the workarounds suggested in my previous reply.

    Regards,
    Atanas Korchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. lee hom
    lee hom avatar
    17 posts
    Member since:
    Jan 2010

    Posted 11 Aug 2015 in reply to Mike Link to this post

    Hello,

          Have you solve the grouping problem of NHibernate 4.0 except twice ToDataSourceResult?   I have the same problem too.

     


    Thanks for help
    Regards

  7. lee hom
    lee hom avatar
    17 posts
    Member since:
    Jan 2010

    Posted 11 Aug 2015 in reply to Atanas Korchev Link to this post

    Hi,

        NHibernate support GroupBy Linq method, but when we use ToDataSourceResult with grouping, it still has some exception, can you fix it?

        Now I can use ToDataSourceResult like this:

    public static DataSourceResult ToGroupingDataSourceResult<TModel, TResult>(this IQueryable<TModel> enumerable, DataSourceRequest request, Func<TModel, TResult> selector)
    {
        var grouping = request.Groups;
        var hasGrouping = grouping != null && grouping.Count > 0;
        if (!hasGrouping)
        {
            return enumerable.ToDataSourceResult(request, selector);
        }
        //Clear the grouping options
        request.Groups = null;
     
        //Execute the query without grouping
        var result = enumerable
            .ToDataSourceResult(request, selector)
            ;
     
        //Restore grouping
        request.Groups = grouping;
     
        //Call GroupBy to do the grouping in memory
        result.Data = result.Data.AsQueryable().GroupBy(grouping);
        return result;
    }

    Regards,

    Thanks

Back to Top
Kendo UI is VS 2017 Ready