0

I'm using Web API + AbpOData + EF and need to calculate some properties of the objects returned from the database on the server.

The basic code looks something like this:

[AbpApiAuthorize(AppPermissions.OData_Permission_Consume)]
public class ActivityLogsController : AbpODataEntityController<ActivityLogs>
{

    [EnableQuery(PageSize = 50000)]
    public override IQueryable<ActivityLogs> Get()
    {
        var objectContext = new MyObjectContext(); //EF

        return objectContext.ActivityLogs.GetAll();
    }
}

I'm just returning values from database, all's fine.

However what I need is to Convert two datetime value to local time. Like below

[AbpApiAuthorize(AppPermissions.OData_Permission_Consume)]
public class ActivityLogsController : AbpODataEntityController<ActivityLogs>
{

    [EnableQuery(PageSize = 50000)]
    public override IQueryable<ActivityLogs> Get()
    {
        var objectContext = new MyObjectContext(); //EF

        return objectContext.ActivityLogs.Select(d => new ActivityLogs()
        {
            Id = d.ID,
            Activity = d.Activity,
            StartTime = d.StartTime.Value.AddHours(5),
            EndTime = d.EndTime.Value.AddHours(5),
            Duration = d.Duration
        });
    }
}

I getting below error

The entity or complex type 'ActivityLogs' cannot be constructed in a LINQ to Entities query.

how i can impliment this using abp odata framework(.net zero). keeping in mind that we need to return the same IQueryable that's returned from EF call.

Shanalal Kasim
  • 131
  • 2
  • 9

1 Answers1

0

The error is caused by impossibility to transform AddHours method to SQL.

You have 2 options:

  • Create a view in DB where you will keep your additional logic.
  • Add your business for DateTime properties in your client side.
  • Thanks for your reply. Basically, I want to convert the start time & end time to client local time (each client time zone stored in settings table). Based on the client the hour will change. so I can't use the first option. This API is using in Power BI so the second option also not workable. – Shanalal Kasim Jul 25 '17 at 07:17