比较两个数组值,但方式不同...... [英] Compare two array values but different way......

查看:73
本文介绍了比较两个数组值,但方式不同......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在比较两个数组值。第一个数组具有时间值(15:34),第二个数组与第一个数组完全相同但没有时间。我想检查两者是否相等,只是跳过时间(15:34)我需要比较其他值。我怎么能这样做?



例如: -

A [] = {是,否,9月14日15:34,未验证}

B [] = {是,否,9月14日14,未经验证}

预期结果:正确



A [] = {是,否,9月14日15:34,未经验证}

B [] = {是,否,9月14日14日,已通过验证}

预期结果:False(即,验证在B []中不同)

I was just comparing the two array values. The first array has the time value (15:34) and the second array have exact same of the first array but without the time. I want to check both are equal, just skip the time (15:34) other values are I need to compare. How can I do this?

For Example:-
A[] = {Yes, No, 15 Sep 14 15:34, Unverified}
B[] = {Yes, No, 15 Sep 14, Unverified}
Expected Result : True

A[] = {Yes, No, 15 Sep 14 15:34, Unverified}
B[] = {Yes, No, 15 Sep 14, verified}
Expected Result : False (i.e., verified is different in the B[])

推荐答案

使用DateTime.Date属性比较时间值:它随时删除组件。



顺便说一下:这是一种非常糟糕的信息存储方式。你最好创建一个类来保存数据并为类编写比较器。

Compare the time values using the DateTime.Date property: it removes any time component.

By the way: that's a pretty poor way to store information. You would be much better off creating a class to hold the data and writing a comparer for the class.
public class MyData
   {
   public bool YesOrNo { get; set; }
   public bool NoOrYes { get; set; }
   public DateTime Date { get; set; }
   public string ExtraData { get; set; }
   }

使用数组来保存不同的数据类型是一种向代码中引入错误和问题的好方法(并且还会使数据的使用复杂化)

using an array to hold different datatypes is a good way to introduce bugs and problems to you code (and complicate the usage of the data as well)


public class MyData1
   {
   public string col1 { get; set; }
   public string col2 { get; set; }
   public DateTime col3 { get; set; }
   public string ExtraData { get; set; }
   }

public class MyData2
   {
   public string col1 { get; set; }
   public string col2 { get; set; }
   public DateTime col3 { get; set; }
   public string ExtraData { get; set; }
   }









比较时,make col3 as



MyData1.col3.ToShortDateString()= MyData2.col3.ToShortDateString()



它会只检查日期





while compare , make col3 as

MyData1.col3.ToShortDateString() = MyData2.col3.ToShortDateString()

it will check only with date


这篇关于比较两个数组值,但方式不同......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆