Pens_Cup87
Pens_Cup87

Reputation: 37

SQL LINQ Convert to String

How do I convert a dateTime SQL to a string that can be formatted? I have not found a way to convert "requestIniationDate" to a string that can be formatted.

protected void Page_Load(object sender, EventArgs e)
{
    //"data" is a database connection through the Data folder, containing the tables
    var data = new Data.AcademicCodeRequestDBEntities();

    var request = data.Requests.Select(x => new 
                        {
                            x.appName, 
                            x.requestIniatiationDate, 
                            x.status, 
                            x.id
                        }).ToList()
                        .Select(x => new Models.RequestIdentifier() 
                        {
                            id = x.id,
                            appName = x.appName,
                            requestIniatiationDate = x.requestIniatiationDate,
                            status = x.status,

                        });
    editGrid.DataSource = request;
    editGrid.DataBind();
}

Upvotes: 0

Views: 188

Answers (5)

Bob K
Bob K

Reputation: 480

How about using .ToString("d")? See https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

protected void Page_Load(object sender, EventArgs e) {
    var data = new Data.AcademicCodeRequestDBEntities();
    var request = data.Requests.Select(x => new 
                    {
                        x.appName, 
                        x.requestIniatiationDate.ToString("d"), 
                        x.status, 
                        x.id
                    }).ToList()
                    .Select(x => new Models.RequestIdentifier() 
                    {
                        id = x.id,
                        appName = x.appName,
                        requestIniatiationDate = x.requestIniatiationDate.ToString("d"),
                        status = x.status,
                    });
    editGrid.DataSource = request;
    editGrid.DataBind();
}

Upvotes: 1

you can implement it like this:

DateTime dt = DateTime.ParseExact(dateString, "ddMMyyyy", CultureInfo.InvariantCulture);

dt.ToString("yyyyMMdd");

Upvotes: 0

maccettura
maccettura

Reputation: 10818

According to your comment, requestIniatiationDate and x.requestIniatiationDate are both DateTime. This is the source of your problem as you cannot assign a string to a DateTime.

A DateTime has no concept of a "format". If you want to display your dates on a page in a specific format you need to use a string to display it. So you need to either modify your RequestIdentifier model to use a string instead of DateTime, or depending on how this is being displayed on the front end you can use a property of the control. For example, if you are using an <asp:boundfield> you can do this:

<asp:boundfield datafield="requestIniatiationDate" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />

Obviously replace with whatever date format you need (you haven't specified).

Besides that, your Linq statement makes very little sense. You materialize a list, then subsequently .Select() on it.. You select into an anonymous object first for no reason, its all very confusing. You should be able to just do this:

var request = data.Requests.Select(x => new Models.RequestIdentifier() 
                    {
                        id = x.id,
                        appName = x.appName,
                        requestIniatiationDate = x.requestIniatiationDate,
                        status = x.status,
                    })
                    .ToList();

Upvotes: 1

Greg
Greg

Reputation: 1116

Assuming reqiestInitiationDate is DateTime and you want the format yyyy-MM-dd

protected void Page_Load(object sender, EventArgs e)
{      
    var data = new Data.AcademicCodeRequestDBEntities();    
    var request = data.Requests
                        .Select(x => new Models.RequestIdentifier() 
                        {
                            id = x.id,
                            appName = x.appName,
                            requestIniatiationDate = x.requestIniatiationDate.ToString("yyyy-MM-dd"),
                            status = x.status,    
                        }).ToList();
    editGrid.DataSource = request;
    editGrid.DataBind();
}

Upvotes: 0

Afonso
Afonso

Reputation: 338

Assuming that requestIniatiationDate is a string, you can do:

protected void Page_Load(object sender, EventArgs e)
{
    //"data" is a database connection through the Data folder, containing the tables
    var data = new Data.AcademicCodeRequestDBEntities();

    var request = data.Requests.Select(x => new 
                        {
                            x.appName, 
                            x.requestIniatiationDate, 
                            x.status, 
                            x.id
                        }).ToList()
                        .Select(x => new Models.RequestIdentifier() 
                        {
                            id = x.id,
                            appName = x.appName,
                            requestIniatiationDate = x.requestIniatiationDate.ToString("yyyy-MM-dd"), //change to the format you want
                            status = x.status,

                        });
    editGrid.DataSource = request;
    editGrid.DataBind();
}

Upvotes: 0

Related Questions