c#数据结构到带有日期时间的索引值
我需要存储数据,以便将c#数据结构到带有日期时间的索引值,c#,C#,我需要存储数据,以便将DateTime值和float值存储在一起。Dictionary没有用处,因为在检索数据时,我没有确切的DateTime值。我只给出日期(不是小时、分钟、秒值),并且必须得到与日期对应的浮点值。我还可以使用什么样的数据结构?为什么不使用一个字典,日期部分作为键,日期时间/浮动元组作为值?我想你必须使用日期时间。此外,如果您不需要时间等,您可以只使用日期部分 DateTime dt = DateTime.Now.Date; 然后,您可以创建字典: Dictionary<
DateTime
值和float
值存储在一起。Dictionary
没有用处,因为在检索数据时,我没有确切的DateTime
值。我只给出日期(不是小时、分钟、秒值),并且必须得到与日期对应的浮点值。我还可以使用什么样的数据结构?为什么不使用一个字典
,日期部分作为键,日期时间/浮动元组作为值?我想你必须使用日期时间。此外,如果您不需要时间等,您可以只使用日期部分
DateTime dt = DateTime.Now.Date;
然后,您可以创建字典:
Dictionary<DateTime, float> dictionary = new Dictionary<DateTime, float>();
您可以将功能包装到自定义集合中。我提供的示例在任何方面都不是最优的,但它可能会让您了解如何使数据更易于使用
public sealed class DateTimeFloatDictionary : IEnumerable<KeyValuePair<DateTime, float>>
{
private readonly Dictionary<DateTime, float> _dictionary;
public float this[DateTime index]
{
get
{
return _dictionary[index.Date];
}
set
{
_dictionary[index] = value;
}
}
public DateTimeFloatDictionary()
{
_dictionary = new Dictionary<DateTime, float>();
}
public bool Contains(DateTime time)
{
return _dictionary.ContainsKey(time.Date);
}
public void Add(DateTime time, float value)
{
_dictionary.Add(time.Date, value);
}
public bool Remove(DateTime time)
{
return _dictionary.Remove(time.Date);
}
#region IEnumerable<KeyValuePair<DateTime,float>> Members
public IEnumerator<KeyValuePair<DateTime, float>> GetEnumerator()
{
return _dictionary.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
公共密封类DateTimeLoadDictionary:IEnumerable
{
私人只读词典;
公共浮动此[日期时间索引]
{
收到
{
返回字典[索引日期];
}
设置
{
_字典[索引]=值;
}
}
公共日期TimeLoadDictionary()
{
_字典=新字典();
}
公共布尔包含(日期时间)
{
return _dictionary.ContainsKey(time.Date);
}
公共无效添加(日期时间、浮动值)
{
_dictionary.Add(时间、日期、值);
}
公共布尔删除(日期时间)
{
return _dictionary.Remove(time.Date);
}
#区域可数成员
公共IEnumerator GetEnumerator()
{
返回_dictionary.GetEnumerator();
}
#端区
#区域可数成员
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回此.GetEnumerator();
}
#端区
}
看看这个,虽然没有经过测试
public class StrippedDate : IComparable<StrippedDate>, IComparable
{
public DateTime Date { get; private set; }
public StrippedDate(DateTime date)
{
Date = date;
}
public int CompareTo(StrippedDate other)
{
//you can simply compare
if (Date.Year == other.Date.Year &&
Date.Month == other.Date.Month &&
Date.Day == other.Date.Day) return 0;
//Put your logic to compare
return Date.CompareTo(other.Date);
}
public override int GetHashCode()
{
//TODO:
//Write better hash logic
return Date.Year.GetHashCode() <<
Date.Month.GetHashCode() <<
Date.Month.GetHashCode() >> 0x41;
}
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
}
public class StripedDateDictionary : IEnumerable<StrippedDate>
{
private readonly Dictionary<StrippedDate, IList<float>> dictionary;
public StripedDateDictionary(Dictionary<StrippedDate, IList<float>> dictionary)
{
this.dictionary = dictionary;
}
public void Add(StrippedDate key, float[] values)
{
dictionary.Add(key, new List<float>(values));
}
public float[] this[StrippedDate index]
{
get
{
return null;
}
set
{
/* set the specified index to value here */
}
}
public float[] this[DateTime time]
{
get
{
StrippedDate date = new StrippedDate(time);
return this[date];
}
set { throw new NotImplementedException("Setter not implemented"); }
}
public IEnumerator<StrippedDate> GetEnumerator()
{
return dictionary.Select(pair => pair.Key).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public类StrippedDate:IComparable,IComparable
{
公共日期时间日期{get;private set;}
公共条带日期(日期时间日期)
{
日期=日期;
}
公共整数比较(条带日期其他)
{
//你可以简单地比较一下
如果(Date.Year==other.Date.Year&&
Date.Month==其他.Date.Month&&
Date.Day==其他.Date.Day)返回0;
//把你的逻辑加以比较
返回日期。比较(其他日期);
}
公共覆盖int GetHashCode()
{
//待办事项:
//编写更好的散列逻辑
返回日期.Year.GetHashCode()0x41;
}
公共整数比较(对象对象对象)
{
抛出新的NotImplementedException();
}
}
公共类StripedDateDictionary:IEnumerable
{
私人只读词典;
公共条带DateDictionary(字典字典)
{
这本字典=字典;
}
public void Add(StrippedDate键,float[]值)
{
添加(键,新列表(值));
}
公共浮动[]此[StrippedDate索引]
{
收到
{
返回null;
}
设置
{
/*将指定的索引设置为此处的值*/
}
}
公共浮动[]此[日期时间]
{
收到
{
StrippedDate=新的StrippedDate(时间);
返回此[日期];
}
set{throw new NotImplementedException(“Setter未实现”);}
}
公共IEnumerator GetEnumerator()
{
返回dictionary.Select(pair=>pair.Key).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}
我每天可以有多个条目。我会有重复的钥匙then@Aks:您应该在问题中提到这一点,因为这对可能的解决方案有很大影响。@Aks:如果您在同一日期有多个条目,那么您希望返回哪个浮动?好的。我已经解决了这个问题。我将在列表中存储一天的所有条目并存储日期。谢谢
public class StrippedDate : IComparable<StrippedDate>, IComparable
{
public DateTime Date { get; private set; }
public StrippedDate(DateTime date)
{
Date = date;
}
public int CompareTo(StrippedDate other)
{
//you can simply compare
if (Date.Year == other.Date.Year &&
Date.Month == other.Date.Month &&
Date.Day == other.Date.Day) return 0;
//Put your logic to compare
return Date.CompareTo(other.Date);
}
public override int GetHashCode()
{
//TODO:
//Write better hash logic
return Date.Year.GetHashCode() <<
Date.Month.GetHashCode() <<
Date.Month.GetHashCode() >> 0x41;
}
public int CompareTo(object obj)
{
throw new NotImplementedException();
}
}
public class StripedDateDictionary : IEnumerable<StrippedDate>
{
private readonly Dictionary<StrippedDate, IList<float>> dictionary;
public StripedDateDictionary(Dictionary<StrippedDate, IList<float>> dictionary)
{
this.dictionary = dictionary;
}
public void Add(StrippedDate key, float[] values)
{
dictionary.Add(key, new List<float>(values));
}
public float[] this[StrippedDate index]
{
get
{
return null;
}
set
{
/* set the specified index to value here */
}
}
public float[] this[DateTime time]
{
get
{
StrippedDate date = new StrippedDate(time);
return this[date];
}
set { throw new NotImplementedException("Setter not implemented"); }
}
public IEnumerator<StrippedDate> GetEnumerator()
{
return dictionary.Select(pair => pair.Key).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}