Home > Net >  Linq2SQL Select in interface for better performance
Linq2SQL Select in interface for better performance

Time:10-03

I'm trying to optimize behavior of my web application.

I'm using interface as base for models, then I have controller, where methods from this interface are used.

For loading more datas to tables I'd need to use Select special for every model inheriting the interface (not to load all fields from database).

Here is code which is working:

public virtual JsonResult DbData(DataTableAjaxPostModel model)
{ 
    var datalist = dbSetForDbData.GetAndSortDataForAJAX(model, true, out int recordsFiltered, out int recordsTotal); //get and sort data

when I'd like to have something like this:

public virtual JsonResult DbData(DataTableAjaxPostModel model)
{ 
    var dataList = dbSetForDbData.Select(SelectDbData).GetAndSortDataForAJAX(model, true, out recordsFiltered, out recordsTotal); //get and sort data

I thought, that I'd use somethink like this:

Expression<Func<T, T>> SelectDbData = input=>IModel<T,TModel>.SelectForModel(input)

Where IModel<T,TModel> is my interface.

In interface I had this method:

public override FooModel SelectForModel(FooModel input)
{
    return new FooModel
    {
        Id = input.Id,
        Number = input.Number,
        ...
    };
}

But it does not work (expression cannot be translated).

CodePudding user response:

You should not break Expression chain. Everything should be Expression

Expression<Func<T, T>> SelectDbData = IModel<T,TModel>.SelectForModel();
public override Exppression<Func<FooModel, FooModel>> SelectForModel()
{
    return input => new FooModel
    {
        Id = input.Id,
        Number = input.Number,
        ...
    };
}
  • Related