Using func while retriving date from database

Wojciech Szabowicz

I have a method that retrieves timestamp intervals from db and it looks like this:

public IEnumerable<DateTime> GetCurrentsFlagTimestampPoints(DateTime startDate, DateTime endDate)
{
    return currentsRepository.GetTimestampPoints(startDate, endDate).Select(timestamp => new DateTime(timestamp.Year, timestamp.Month, timestamp.Day)).Distinct();
}

But now I want to use it more in a more generic way, so i want to pass Func<> that will describe resulting date format so once I want to return

new DateTime(timestamp.Year, timestamp.Month, timestamp.Day)

and once

new DateTime(timestamp.Year, timestamp.Month, timestamp.Day, timestamp.Hour)

I could do that using overloads of some sort but that would change the already existing structure of some methods.

I don't know how to tackle this, is it possible??

Edit:

It works nice without Expression so:

    public IEnumerable<DateTime> GetCurrentsFlagTimestampPoints(DateTime startDate, DateTime endDate, Func<DateTime, DateTime> dateTimeExpr)
    {
        return currentsRepository.GetTimestampPoints(startDate, endDate).Select(dateTimeExpr).Distinct();
    }
Amir Popovich

You need to pass a Expression<Func<DateTime,DateTime>> as a param:

public IEnumerable<DateTime> GetCurrentsFlagTimestampPoints(DateTime startDate, DateTime endDate, Expression<Func<DateTime, DateTime>> dateTimeExpr)
{
    return currentsRepository.GetTimestampPoints(startDate, endDate).Select(dateTimeExpr).Distinct();
}

Expression<Func<DateTime, Datetime>> f1 = timestamp => new DateTime(timestamp.Year, timestamp.Month, timestamp.Day);

Expression<Func<DateTime, Datetime>> f1 = timestamp => new DateTime(timestamp.Year, timestamp.Month, timestamp.Day, timestamp.Hour);

GetCurrentsFlagTimestampPoints(DateTime.Now, DateTime.Now, f1); // or f2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

retriving image from from database through mysqli using php is not working

From Dev

Uploading and retriving image from mysql database

From Dev

Retriving 3 last records from database for each id

From Dev

Redirect and 'field' keyword issues when retriving objects from database

From Dev

Retriving values from generated text box using jquery

From Dev

change date format while displaying date column from database Yii

From Dev

How to get File extension while retriving saved Files from MS SQL Server2008 to local drive

From Dev

How to get File extension while retriving saved Files from MS SQL Server2008 to local drive

From Dev

Retriving data from different tables in database sending SQL code to be executed, returning data?

From Dev

Format date from database to table using php

From Dev

Using Date functions to pull records from database

From Dev

Searching and Retriving image from a folder

From Dev

cakephp retriving values from combobox

From Dev

Formatting date while retrieving datetime column from database Entity framework

From Dev

Error while retriving the last row of a table

From Dev

Error while retriving the last row of a table

From Dev

Saving / Retriving all the fields value's of an HTML form into / from a local file using JavaScript

From Dev

Using TOP, Limit while fetching data using ID from database

From Dev

Error while adding Date to Database

From Dev

google Places API retriving an photos using getPlacePhotos

From Dev

Retriving item by id Firebase using AngularFire

From Dev

Retriving item by id Firebase using AngularFire

From Dev

google Places API retriving an photos using getPlacePhotos

From Dev

Inconsistent date format while retrieving from postgres using java 1.7

From Dev

get admin credentials from liferay while using hsql database

From Dev

How To Skip Some Fields From Database While Using fputcsv

From Dev

get admin credentials from liferay while using hsql database

From Dev

Unable To fetch data from database to script tag while using graph

From Dev

Using Where cluase while loading data in Qlikview From Database

Related Related

  1. 1

    retriving image from from database through mysqli using php is not working

  2. 2

    Uploading and retriving image from mysql database

  3. 3

    Retriving 3 last records from database for each id

  4. 4

    Redirect and 'field' keyword issues when retriving objects from database

  5. 5

    Retriving values from generated text box using jquery

  6. 6

    change date format while displaying date column from database Yii

  7. 7

    How to get File extension while retriving saved Files from MS SQL Server2008 to local drive

  8. 8

    How to get File extension while retriving saved Files from MS SQL Server2008 to local drive

  9. 9

    Retriving data from different tables in database sending SQL code to be executed, returning data?

  10. 10

    Format date from database to table using php

  11. 11

    Using Date functions to pull records from database

  12. 12

    Searching and Retriving image from a folder

  13. 13

    cakephp retriving values from combobox

  14. 14

    Formatting date while retrieving datetime column from database Entity framework

  15. 15

    Error while retriving the last row of a table

  16. 16

    Error while retriving the last row of a table

  17. 17

    Saving / Retriving all the fields value's of an HTML form into / from a local file using JavaScript

  18. 18

    Using TOP, Limit while fetching data using ID from database

  19. 19

    Error while adding Date to Database

  20. 20

    google Places API retriving an photos using getPlacePhotos

  21. 21

    Retriving item by id Firebase using AngularFire

  22. 22

    Retriving item by id Firebase using AngularFire

  23. 23

    google Places API retriving an photos using getPlacePhotos

  24. 24

    Inconsistent date format while retrieving from postgres using java 1.7

  25. 25

    get admin credentials from liferay while using hsql database

  26. 26

    How To Skip Some Fields From Database While Using fputcsv

  27. 27

    get admin credentials from liferay while using hsql database

  28. 28

    Unable To fetch data from database to script tag while using graph

  29. 29

    Using Where cluase while loading data in Qlikview From Database

HotTag

Archive