如何比较一个数组中的日期 [英] how to compare dates in one array
问题描述
i有一个包含重复日期的数组。我希望结果作为日期没有复制
ex 12/12 / 2013
12/12/2013
12/12/2013
12/12/2013
01 / 01/2014
01/02/2014
12/12/2013
11/11/2013
11/11/2013
10/10/2013
结果应为
12/12 / 2013
01/01/2014
01/02/2014
12/12/2013
11 / 11/2013
10/10/2013
请帮我这样做。提供任何示例代码
请您在下面的链接中查看..
http://weblogs.asp.net/hajan/archive/2010/09/20/ get-distinct-values-of-array-using-linq.aspx [ ^ ]
HI Kanna,
您可以尝试以下代码,
将为您做到这一点
string [] dateWithDup = new string [] { 12/12/2013, 12/12/2013, 12/12/2013, 12/12/2013, 01/01/2014, 01/02/2014, 12 / 12/2013, 11/11/2013, 11/11/2013,< span class =code-string> 10/10/2013};
string [] dateWithoutDup = new string [ 50 ];
int count = 0 ;
foreach ( var date in dateWithDup)
{
if (!dateWithoutDup.Contains(date))
{
dateWithoutDup [count] =日期;
count ++;
}
}
希望这会对你有所帮助。
问候,
RK
Tejas Vaishnav的解决方案1非常好。我会提供另一个linq查询:
DateTime [] dt = { new DateTime( 2013 , 12 , 12 ), new DateTime( 2013 , 12 , 12 ),
new DateTime( 2013 , 12 , 12 ), new DateTime( 2013 , 12 , 12 ),
new DateTime( 2014 , 01 , 01 ), new DateTime( 2014 , 01 , 02 ),
new DateTime(< span class =code-digit> 2013 , 12 , 12 ), new DateTime( 2013 , 11 , 11 ),
new DateTime( 2013 , 11 , 11 ), new DateTime ( 2013 , 10 , 10 )} ;
var uniques = dt.GroupBy(g => g)。其中(g => g.Count()> = 1 )。选择(g => g.Key);
foreach (日期时间d 唯一身份)
{
控制台。的WriteLine(d.ToString());
}
Console.ReadKey();
不同的方法 [ ^ ]当然是最简单的(最快?)方式。
要仅选择重复项,请更改其中
声明:
其中(g => g.Count()> 1)
要选择唯一值(非重复),更改其中
声明:
其中(g => g.Count()== 1 跨度>)
hi,
i have a array which contains dates with duplications.i want result as dates with out duplication
ex 12/12/2013
12/12/2013
12/12/2013
12/12/2013
01/01/2014
01/02/2014
12/12/2013
11/11/2013
11/11/2013
10/10/2013
result should be
12/12/2013
01/01/2014
01/02/2014
12/12/2013
11/11/2013
10/10/2013
please help me to do this.provide any sample code
can you please check it out below links..
http://weblogs.asp.net/hajan/archive/2010/09/20/get-distinct-values-of-an-array-using-linq.aspx[^]
HI Kanna,
You could try this below code,
will do the trick for you
string[] dateWithDup =new string[]{ "12/12/2013","12/12/2013","12/12/2013","12/12/2013","01/01/2014","01/02/2014","12/12/2013","11/11/2013","11/11/2013","10/10/2013"}; string[] dateWithoutDup = new string[50]; int count = 0; foreach (var date in dateWithDup) { if (!dateWithoutDup.Contains(date)) { dateWithoutDup[count] = date; count++; } }
Hope this helps you a bit.
Regards,
RK
Solution 1 by Tejas Vaishnav is very good. I would provide another linq query:
DateTime[] dt = {new DateTime(2013,12,12), new DateTime(2013,12,12), new DateTime(2013,12,12), new DateTime(2013,12,12), new DateTime(2014,01,01), new DateTime(2014,01,02), new DateTime(2013,12,12), new DateTime(2013,11,11), new DateTime(2013,11,11), new DateTime(2013,10,10) }; var uniques = dt.GroupBy(g=>g).Where(g=>g.Count()>=1).Select(g=>g.Key); foreach (DateTime d in uniques) { Console.WriteLine(d.ToString()); } Console.ReadKey();
Distinct method[^] is - of course - an easiest (and fastest?) way.
To select only duplicates, changeWhere
statement:
Where(g=>g.Count()>1)
To select uniques values (non-duplicates), changeWhere
statement:
Where(g=>g.Count()==1)
这篇关于如何比较一个数组中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!