AlexanderKrutov / DataTables.Queryable

.Net library for clever processing of requests from datatables.net jQuery plugin on the server side (ASP.NET, Nancy or any other web server).
MIT License
51 stars 19 forks source link

Using the library with in-memory objects #23

Closed pskrtr closed 6 years ago

pskrtr commented 6 years ago

Hi,

I have a model with some calculated fields that must be searched but since they do not exist in the schema naturaly had a System.NotSupportedException: 'The specified type member 'FieldName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.' thrown at https://github.com/AlexanderKrutov/DataTables.Queryable/blob/80398b19897ee92f7b08eaa48a9a38874425c4b7/DataTables.Queryable/DataTablesQueryable.cs#L84

I am aware of the purpose of this library, the intention is relying on data context.

So my question is, what would be the path to use the library with in-memory objects e.g. List<MyMode> ? My dataset is not too big so it won't hurt. Any tips?

AlexanderKrutov commented 6 years ago

Hi @pskrtr Could you please explain more detailed the problem you trying to solve? It will be good if you share some piece of code with sample data model.

pskrtr commented 6 years ago

Thanks for the reply @AlexanderKrutov, sure.

Here's an example model to demostrate the issue.

public class Person
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [MaxLength(50)]
    public string Name       { get; set; } 
    [MaxLength(200)]
    public string Description { get; set; }

    public string ReversedName => string.Join("", Name.Reverse());
}

Id, Name and Description are existing in my table. ReversedName on the other hand is a calculated field as you can see. The library has no problem with a default DataTables view, displaying and pagination just works. However when it comes to search something or sort by a column, this calcuated field leads the exception. That's why I'm asking another way to use the library with an in-memory set, e.g.;

public class MyController : ApiController
{
    public JsonResult<object> Get()
    {
        var request = new DataTablesRequest<Person>(Request.RequestUri.Query);
        using (var ctx = new MyContext())
        {
            // fetch all table to filter nor sort in app, not via linq to entities
            var personsInMemory = ctx.Persons.ToList();             

            // this of course does not work
            var persons = personsInMemory.ToPagedList(request); 

            var response = new
            {
                recordsTotal = persons.TotalCount,
                recordsFiltered = persons.TotalCount,
                data = persons
            };
            return new JsonResult ... ;
        }
    }
}

I hope I can explained well.

AlexanderKrutov commented 6 years ago

@pskrtr got it. Try this:

var personsInMemory = ctx.Persons.ToList();                     
var persons = personsInMemory.AsQueryable().AsDataTablesQueryable(request).ToPagedList(request);
AlexanderKrutov commented 6 years ago

@pskrtr hope my previous advice helped to solve your issue, so I'm going to close the case. If no please just reopen it.

pskrtr commented 6 years ago

@AlexanderKrutov It worked flawlessly, thank you so much.