在Lambda表达式中更改DateTime格式 [英] Changing DateTime format in lambda Expression

查看:235
本文介绍了在Lambda表达式中更改DateTime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DateTime格式的DTO字段

I have a DTO field in DateTime format

public DateTime date_joined { get; set; }

我用它来将数据转换为Json

I use this to turn the data into a Json

public JsonResult Customer(int? id)
        {
            var user = User.Identity.Name;
            if (!AccountController.IsInRole(user, "admin"))
            {
                return null;
            }

            id = id == null ? 0 : id;

            var customer = db.PrmCustomers
                .Where(x => x.EmailAddress != null && x.CustomerID > id)
                .OrderBy(x => x.CustomerID)
                .Select(x => new CustomerDTO()
                {
                    email = x.EmailAddress,
                    phone = x.MobilePhoneNumber,
                    username = "",
                    first_name = x.Name,
                    last_name = x.Surname,
                    gender = x.GenderType,
                    customer_code = x.CustomerID,
                    is_active = "",
                    //date_joined = String.Format("{0:d/M/yyyy HH:mm:ss}", x.CreateDate.ToString()),
                    date_joined = x.CreateDate,
                    last_login = "",
                    date_of_birth = x.BirthDate,
                    //date_of_birth = String.Format("{0:d/M/yyyy HH:mm:ss}", x.BirthDate.ToString()),
                    password = x.Password,
                    email_allowed = x.IsNewsletter,
                    sms_allowed = x.IsSms,
                    verified = x.IsApprovedEmail,
                    social_account_facebook_uuid = "",
                    social_account_facebook_extra = ""
                });

            return Json(customer, JsonRequestBehavior.AllowGet);
        }

问题是这样

"date_joined":"\/Date(1516965473683)\/"

到目前为止,我试图将其更改为另一种格式.

I tried to change it into another format so far I couldn't manage it.

首先,我尝试将通常的DateTime格式设置为toString("newFOrmat"),但出现Linq错误的主要原因是,在SQL Server中无法识别toString()

First I tried the usual DateTime formatting as toString("newFOrmat") but I got Linq errors mostly because toString() is not recognized in SQL Server

然后我遇到了这个问题 linq查询结果中的格式日期,尝试了这种方法

Then I came across this question format date in linq query result and tried the method there as this

return Json(customer.AsEnumerable().Select(r=>new CustomerDTO{
                date_joined = r.date_joined.GetValueOrDefault().ToString("dd.MM.yyyy")
            }) , JsonRequestBehavior.AllowGet);

尽管包含正确的命名空间,但出现了"DateTime没有GetValueOf()的定义"错误.

I got "DateTime does not have a definition for GetValueOf()" error although I have the correct namespaces included.

省略它并仅使用常规的ToString("format")会导致上面的Linq错误.

Omitting it and using only the usual ToString("format") brought the Linq error above.

还有其他建议吗?

我的DTO和输出也有其他字段.我没有在问题中加入他们.

My DTO and Output has other fields too. I didn't include them in the question.

推荐答案

我认为您最好的选择是通过调用.ToList()从数据库中加载所需的数据,然后将该数据映射到您的DTO. > 确保将DTO上的类型从DateTime更改为string.

I think your best bet here is to load in the required data from the database by calling .ToList(), and then map that data to your DTO.
Make sure to change the type on your DTO from DateTime to string.

类似这样的东西:

public JsonResult Customer(int? id)
    {
        var user = User.Identity.Name;
        if (!AccountController.IsInRole(user, "admin"))
        {
            return null;
        }

        id = id == null ? 0 : id;

        var customer = db.PrmCustomers
            .Where(x => x.EmailAddress != null && x.CustomerID > id)
            .OrderBy(x => x.CustomerID).ToList()  //  <-- ToList executes the query
            .Select(x => new CustomerDTO()
            {
                email = x.EmailAddress,
                phone = x.MobilePhoneNumber,
                username = "",
                first_name = x.Name,
                last_name = x.Surname,
                gender = x.GenderType,
                customer_code = x.CustomerID,
                is_active = "",
                date_joined = x.CreateDate.ToString("d/M/yyyy HH:mm:ss"),
                //date_joined = x.CreateDate,
                last_login = "",
                //date_of_birth = x.BirthDate,
                date_of_birth = x.BirthDate.ToString("d/M/yyyy HH:mm:ss"),
                password = x.Password,
                email_allowed = x.IsNewsletter,
                sms_allowed = x.IsSms,
                verified = x.IsApprovedEmail,
                social_account_facebook_uuid = "",
                social_account_facebook_extra = ""
            });

        return Json(customer, JsonRequestBehavior.AllowGet);
    }

这篇关于在Lambda表达式中更改DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆