[英]Format string and return as anonymous type C#
I have the folowing code 我有以下代码
var details = GetDetails();
return details; //array of objects
This contains a proterty details[0].LocationTime
which is of datatype DateTime. 它包含一个属性details[0].LocationTime
,其数据类型为DateTime。 I need to format the date and return it as string using a utility method like FormatDate(details[0].LocationTime)
. 我需要格式化日期并使用诸如FormatDate(details[0].LocationTime)
类的实用工具方法将其返回为字符串。 So the return object will have details.LocationTime
as a string datatype and not DateTime. 因此,返回对象将具有details.LocationTime
作为字符串数据类型,而不是DateTime。
How can I do this using anonymous types and LINQ. 如何使用匿名类型和LINQ执行此操作。 Without creating another class and using a for loop. 无需创建另一个类并使用for循环。
Thanks in advance 提前致谢
var details = GetDetails();
return details.Select(d => new { LocationTime = FormatDate(d.LocationTime) });
It works if your FormatDate()
method returns string. 如果您的FormatDate()
方法返回字符串,则该方法有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.