我有一个LINQ表达式,它将Dateformat从UTC转换为用户文化的日期格式。
for (int i = 0; i < datexml.count ; i++)
{
if (datatype.ToLower() == "date")
{
myList.Select(c => { ((object[])(c))[i] = ConvertFromUTCDate( Convert.ToDateTime (((object[])(c))[i]), UserTimeZone); return c; }).ToList();
}
}
有些时候(object [])(c))[i]中的日期值可能为null,或者如果值错误地存储在DB中,则为字符串或小数。
如何检查值是否为空且具有日期,然后将其转换为此表达式。
答案 0 :(得分:3)
为了避免增加更多复杂性并且能够轻松读取和维护代码,请提取匿名方法并使其成为命名方法
来自
c =&gt; {((object [])(c))[i] = ConvertFromUTCDate(Convert.ToDateTime (((object [])(c))[i]),UserTimeZone);返回c; }
到
Error 2 'Plugin.QueryResponseMessage.Types.Response' does not contain a definition for 'Types' C:\vs_projects\NSClientHelpers\NsClientPlugin\SamplePlugin.cs 45 74 NsClientPlugin
Error 1 'Plugin.QueryResponseMessage.Types.Response.Builder' does not contain a definition for 'AddLines' and no extension method 'AddLines' accepting a first argument of type 'Plugin.QueryResponseMessage.Types.Response.Builder' could be found (are you missing a using directive or an assembly reference?) C:\vs_projects\NSClientHelpers\NsClientPlugin\SamplePlugin.cs 45 22 NsClientPlugin
和
public DateTime ConvertFromObjectToDate(object dbdate){
if(dbdate is null || !(dbdate is DateTime))return DateTime.MinValue;
var result = ConvertFromUTCDate(Convert.ToDateTime (dbdate),UserTimeZone);
return result;
}