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

[Solved] Unable to use an enum as column with the IQueryable datasource

4 Answers 222 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Julien
Top achievements
Rank 1
Julien asked on 09 May 2012, 10:52 AM

For web application in development(ASP.Net MVC), I'm using the telerik grid. The grid is bound to an IQueryable of my list, because it's a big table, and I want that telerik apply it's filter on the list, and then executes this result, not dowloading 10'000 rows(with the joined tables), and then with the filter, use only rows.

I'm using(and I really need it for this page, it's one of the key feature) the filter/order of the grid.

One of the main column(determining the kind of the data) is an enum.

The problem is that I get a "Specified type member is not supported in linq to entities"as soon as I'm trying to filter/sort it.

I've to bind it on the enum(and not the mapped int) because if I use the id, filters/order by will be on an int, and I can't expect that the user knows the id of the foreign table.

I just cannot implement myself again all grids parameter(located in url)(I assume, it's either I do everything, or nothing) and filter it correctly, order it correctly).

Do you have an idea of workaround?

4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 14 May 2012, 11:59 AM
Hello Julien,

This is limitation of Entity Framework. Instead of binding to an enumeration, I can suggest to bind the column to the Text property of the foreign entity so that filtering and sorting will not be performed using the text.
Similar scenario is covered in this demo in which a column is bound directly to the Customer.ContactName property.


Regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Julien
Top achievements
Rank 1
answered on 14 May 2012, 12:17 PM
Like I said, filtering on the enum is a key feature of this list.

I know exactly why this problem happens, but I need to find a workaround.

Is there a way, to handle SOME of the parameters received myself?

I mean, check if it's a column named XYZ and they apply myself the filter on the IQueryable?
0
Daniel
Telerik team
answered on 16 May 2012, 03:22 PM
Hi Julien,

Generally speaking, applying the filter to only to certain fields is not supported but can be achieved by using the following approach:
  1. Use Custom Binding for the Grid.
  2. Apply the filters to the needed properties and remove their FilterDescriptors.
  3. Use the ToGridModel IQueryable extension method from the Telerik.Web.Mvc.Extensions namespace to apply the paging, grouping, sorting and the rest of the filters to the data.

Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
0
Stef Heyenrath
Top achievements
Rank 1
answered on 27 Nov 2012, 08:44 PM
You can use PropertyTranslator in combination with QueryIntercepter to add support for enums or other custom translations. See also some example code here.

namespace KendoGridBinder.Examples.MVC.Data.Entities
{
    [Table("KendoGrid_Employee")]
    public class Employee : Entity
    {
        private static readonly CompiledExpressionMap<Employee, bool> IsManagerExpr =
            DefaultTranslationOf<Employee>.Property(e => e.IsManager).Is(e => e.Email.Contains("smith"));
 
        private static readonly CompiledExpressionMap<Employee, EmploymentType> EmploymentTypeExpr =
            DefaultTranslationOf<Employee>.Property(e => e.EmploymentType).Is(e => e.Email.Contains("smith") ? EmploymentType.Manager : EmploymentType.Employee);
 
        private static readonly CompiledExpressionMap<Employee, string> FullNameExpr =
            DefaultTranslationOf<Employee>.Property(e => e.FullName).Is(e => e.FirstName + " " + e.LastName);
 
        public int EmployeeNumber { get; set; }
 
        public string FirstName { get; set; }
 
        public string LastName { get; set; }
 
        public string Email { get; set; }
 
        public DateTime HireDate { get; set; }
 
        public Company Company { get; set; }
 
        [NotMapped]
        public bool IsManager
        {
            get
            {
                return IsManagerExpr.Evaluate(this);
            }
        }
 
        [NotMapped]
        public EmploymentType EmploymentType
        {
            get
            {
                return EmploymentTypeExpr.Evaluate(this);
            }
        }
 
        [NotMapped]
        public string FullName
        {
            get
            {
                return FullNameExpr.Evaluate(this);
            }
        }
    }
}
Tags
Grid
Asked by
Julien
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Julien
Top achievements
Rank 1
Stef Heyenrath
Top achievements
Rank 1
Share this question
or