This is a migrated thread and some comments may be shown as answers.

ToDataSourceResult Problems with NHibernate in a Grid

5 Answers 221 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 24 Feb 2014, 01:53 PM
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.

5 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 26 Feb 2014, 12:52 PM
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!
0
Mike
Top achievements
Rank 1
answered on 26 May 2014, 09:57 PM
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
0
Atanas Korchev
Telerik team
answered on 28 May 2014, 07:34 AM
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!
 
0
lee hom
Top achievements
Rank 1
answered on 11 Aug 2015, 02:28 PM

Hello,

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

 


Thanks for help
Regards

0
lee hom
Top achievements
Rank 1
answered on 11 Aug 2015, 02:48 PM

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

Tags
Data Source
Asked by
Frank
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Mike
Top achievements
Rank 1
lee hom
Top achievements
Rank 1
Share this question
or