使用lambda表达式将DateTime格式转换为MMMM yyyy,dd [英] Convert DateTime format to MMMM yyyy, dd using lambda expression

查看:882
本文介绍了使用lambda表达式将DateTime格式转换为MMMM yyyy,dd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期格式更改为MM:yyyy:dd(2018年10月24日),我尝试了我知道的每种方法,但是它不起作用...这是我的代码(我是许多方法之一上线):

I am trying to change the date format to MM:yyyy:dd (October 2018, 24), I've tried every method I know, but it's not working... here is my code(one of many method I got online):

var data = (from o in db.tbPATxns
                            join p in db.providers on o.providerid equals p.providertin
                            join a in db.Endorsements on o.panumber equals a.panumber
                            join b in db.members on o.IID equals b.legacycode
                            join c in db.proceduredatas on a.proccode equals c.procedurecode
                            where c.proceduredesc.ToLower().Contains("admission")
                            select new
                            {
                                o.panumber,
                                b.legacycode,
                                b.lastname,
                                b.firstname,
                                b.phone1,
                                p.providername,
                                a.txndate.ToString("MMMM yyyy, dd")
                            });

                RadGrid1.DataSource = data.ToList();
                RadGrid1.DataBind();

该查询假设是从数据库中获取所有必填字段,所以我想要将日期时间从数据库转换为(MMMM yyyy,dd),但是toString返回2种类型的错误.

that query is suppose to get all the required field from the database, so what I want is to convert that datetime from database to (MMMM yyyy, dd), but the toString is returning 2 type of errors.

(1)

方法'ToString'的重载不接受1个参数

No Overload For Method 'ToString' takes 1 arguments

(2)

无效的匿名类型成员声明符.匿名类型成员必须 通过成员分配,简单名称或成员访问权限进行声明.

Invalid anonymous type member declarator. Anonymous type members must be declare with a member assignment, simple name or member access.

如果我删除ToString,输出将为

6/21/2018 4:40:15 PM

推荐答案

您可以尝试通过AsEnumerable照原样获取记录,然后将日期格式应用于预计的记录,例如

You could try to fetch your records as it is by AsEnumerable and then apply date formatting on projected records like

select new
{
   o.panumber,
   b.legacycode,
   b.lastname,
   b.firstname,
   b.phone1,
   p.providername,
   a.txndate       //as it is
})
.AsEnumerable()
// now convert
.Select(a => new
{
   panumber = a.panumber,
   legacycode = a.legacycode,
   lastname = a.lastname,
   firstname = a.firstname,
   phone1 = a.phone1,
   providername = a.providername,
   txndate = a.txndate.Value.ToString("MMMM yyyy, dd")
});

这篇关于使用lambda表达式将DateTime格式转换为MMMM yyyy,dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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