0

I have a linq which a simple where statement and a simple select. If i return the result as .ToList() everything works fine. If I change it to .ToDataSourceResult(request), I get exception with the error message "The specified type member 'Disabled' is not supported in LINQ to Entities only when using..." but I can't find where the 'Disabled' is coming from, and why this happens only for DataSourceResult. Any ideas why this is happening or how to solve this?

Here is my Code:

public ResponseData<DataSourceResult> GetMunicipalitiesDDL(DataSourceRequest p_request, string p_filterText)
{
    var response = new ResponseData<DataSourceResult>();

    try
    {
        using (var context = new DBEntities())
        {
            response.Data = (from x in context.MUNICIPALITY_VIEW
                             where x.AC_MUNICIPALITY != null
                             select new SelectListItem
                             {
                                 Text = x.AC_MUNICIPALITY,
                                 Value = x.AC_MUNICIPALITY

                             }).ToDataSourceResult(p_request);
        }
    }
    catch (Exception ex)
    {
        response.Status = ResponseStatus.Unsuccessful;
        response.Message = ex.Message;
    }

    return response;
}
  • What if you try `.ToList().ToDataSourceResult(p_request);`, does it work? – Yong Shun Mar 30 '22 at 07:04
  • 1
    Does this answer your question? [The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported](https://stackoverflow.com/questions/11584660/the-specified-type-member-is-not-supported-in-linq-to-entities-only-initializer) – Berkay Yaylacı Mar 30 '22 at 07:06
  • @YongShun I do not want to use .ToList() because the data are too many and the view uses groupping which takes 1min to bring all the data. So I want to use .ToDataSourceResult() to use virtual scrolling so i can load only the first 20 records and when the user scrolls down, to load the rest. – Pantelitsa Mavrovounioti Mar 30 '22 at 07:15

1 Answers1

0

I have found the solution here https://entityframework.net/knowledge-base/10159539/the-specified-type-member-is-not-supported-in-linq-to-entities--only-initializers--entity-members--and-entity-navigation-properties-are-supported.

What I did was I included the "Disabled" property in the select. I don't know why when using .ToDataSourceResult() I have to select "Disabled" but when using .ToList() it is ok.

My code now looks like this:

public ResponseData<DataSourceResult> GetMunicipalitiesDDL(DataSourceRequest p_request, string p_filterText)
{
    var response = new ResponseData<DataSourceResult>();

    try
    {
        using (var context = new DBEntities())
        {
            response.Data = (from x in context.MUNICIPALITY_VIEW
                             where x.AC_MUNICIPALITY != null
                             select new SelectListItem
                             {
                                 Text = x.AC_MUNICIPALITY,
                                 Value = x.AC_MUNICIPALITY,
                                 Disabled = false

                             }).ToDataSourceResult(p_request);
        }
    }
    catch (Exception ex)
    {
        response.Status = ResponseStatus.Unsuccessful;
        response.Message = ex.Message;
    }

    return response;
}