Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于字符串比较的DateTime.TryParseExact方法_C#_Linq - Fatal编程技术网

C# 用于字符串比较的DateTime.TryParseExact方法

C# 用于字符串比较的DateTime.TryParseExact方法,c#,linq,C#,Linq,嘿,如何对给定日期进行字符串比较匹配,DateTime.TryParseExact似乎是明智的选择,但我不确定如何使用以下方法构造参数: public List<Dates> DateEqualToThisDate(string dateentered) { List<Dates> date = dates.Where( n => string.Equals(n.DateAdded, d

嘿,如何对给定日期进行字符串比较匹配,
DateTime.TryParseExact
似乎是明智的选择,但我不确定如何使用以下方法构造参数:

public List<Dates> DateEqualToThisDate(string dateentered)
{
    List<Dates> date = dates.Where(
        n => string.Equals(n.DateAdded, 
                           dateentered,
                           StringComparison.CurrentCultureIgnoreCase)).ToList();
        return hiredate;
 }
public List DateEqualToThisDate(字符串dateentered)
{
列表日期=日期。其中(
n=>string.Equals(n.DateAdded,
输入日期,
StringComparison.CurrentCultureIgnoreCase().ToList();
回租;
}

如果您准确地知道日期/时间的格式(即它从不改变,并且不依赖于用户的文化或区域设置),则可以使用

例如:

DateTime result;
if (DateTime.TryParseExact(
    str,                            // The string you want to parse
    "dd-MM-yyyy",                   // The format of the string you want to parse.
    CultureInfo.InvariantCulture,   // The culture that was used
                                    // to create the date/time notation
    DateTimeStyles.None,            // Extra flags that control what assumptions
                                    // the parser can make, and where whitespace
                                    // may occur that is ignored.
    out result))                    // Where the parsed result is stored.
{
    // Only when the method returns true did the parsing succeed.
    // Therefore it is in an if-statement and at this point
    // 'result' contains a valid DateTime.
}
格式字符串可以是完全指定的(例如
dd-MM-yyyy
),也可以是(例如
g
)。对于后者来说,日期的格式与文化有关。例如,在荷兰日期写为
26-07-2012
dd-MM-yyyy
),而在美国日期写为
7/26/2012
M/d/yyyy


但是,只有当字符串
str
仅包含要解析的日期时,这一切才起作用。如果有一个更大的字符串,其中包含各种不需要的字符,那么必须首先在其中找到日期。这可以使用正则表达式来完成,而正则表达式本身就是另一个主题。可以找到一些关于C#中正则表达式(regex)的一般信息。正则表达式引用不可用。例如,可以使用正则表达式
\d{1,2}\/\d{1,2}\/\d{4}

找到类似于
d/M/yyyyy
的日期。另一种方法是将日期从
字符串
转换为
日期时间
。如果可能,我会将
DateAdded
保留为
DateTime

Bellow是在LINQPad中运行的代码

public class Dates
{
    public string DateAdded { get; set; }
}

List<Dates> dates = new List<Dates> {new Dates {DateAdded = "7/24/2012"}, new Dates {DateAdded = "7/25/2012"}};

void Main()
{
    DateEqualToThisDate("7/25/2012").Dump();
}

public List<Dates> DateEqualToThisDate(string anything)
{
    var dateToCompare = DateTime.Parse(anything);

    List<Dates> hireDates = dates.Where(n => DateTime.Parse(n.DateAdded) == dateToCompare).ToList();

    return hireDates;
}
公开课日期
{
公共字符串DateAdded{get;set;}
}
列表日期=新列表{新日期{DateAdded=“7/24/2012”},新日期{DateAdded=“7/25/2012”};
void Main()
{
DateEqualToThisDate(“2012年7月25日”).Dump();
}
公共列表DateEqualToThisDate(字符串任意)
{
var dateToCompare=DateTime.Parse(任何内容);
List hireDates=dates.Where(n=>DateTime.Parse(n.DateAdded)==dateToCompare.ToList();
返回受雇者;
}

对于使用LINQ查询语法的人来说,可能重复有用的参考资料