我有三个日期
DateTime date1 = st.UpdatedDate;
DateTimed date2 = cl.updatedDate;
DateTime date3 = d.UpdatedDate;
我正在比较并找到最新的日期如下......
if (date1 > date2 && date1 > date3 )
latestDate = date1;
else if (date2 > date1 && date2 > date3 )
latestDate = date2 ;
else
latestDate = date3;
我想知道并且想问一下有没有内置方法可以比较多个日期并告诉我最佳日期?
答案 0 :(得分:4)
Linq Enumerable.Max
可以为您提供帮助
DateTime result = new[] { date1, date2, date3 }.Max();
答案 1 :(得分:1)
如果您不想使用Enumerable
:
var max = date1;
if (date2 > max) max = date2;
if (date3 > max) max = date3;
...
但我不建议这样做。
答案 2 :(得分:0)
if you have two dates then there is an inbuilt function in C#
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
List<List<string>> myList = new List<List<string>>();
string[] words = new string[] { "trees", "bike", "cars", "steer", "arcs" };
myList = GetAnagrams(words);
private List<List<string>> GetAnagrams(string[] words)
{
return words.GroupBy(w => new string(w.OrderBy(c => c).ToArray())).Select(group => group.ToList()).ToList();
}
答案 3 :(得分:0)
尊敬的Enumerable.OrderByDescending(date => date).First()
(或OrderBy().Last()
)。不仅仅需要,但在某些情况下需要它,而且感觉非常相关!