我有一个简单的类定义如下:
Class Data
{
public datetime DATES {get;set;}
public double ValueA {get;set;}
public double ValueB {get;set;}
}
Dictionary<double, Data> DictA= new Dictionary<double, Data>();
Dictionary<double, Data> DictB= new Dictionary<double, Data>();
DictA 中的值如下所示; (日期,价值A)
1/1/2001, 100
2/1/2001, 150
3/1/2001, 400
DictB 中的值如下所示;的(日期,VALUEB)
2/1/2001, 200
3/1/2001, 400
4/1/2001, 300
5/1/2001, 150
我还有一个observable collection
:
ObservableCollection<Data> curveResults = new ObservableCollection<Data>();
public ObservableCollection<Data> CurveResults
{
get { return curveResults; }
set
{
curveResults = value;
base.OnPropertyChanged("CurveResults");
}
}
我希望将该集合填充为
Dates, ValueA, ValueB
1/1/2001, 100, null
2/1/2001, 150, 200
3/1/2001, 400, 400
4/1/2001, null, 300
5/1/2001, null, 150
答案 0 :(得分:0)
如果要为双重属性设置空值,则类数据应如下所示:
class Data
{
public DateTime DATES { get; set; }
public double? ValueA { get; set; }
public double? ValueB { get; set; }
}
以下是代码:
Dictionary<double, Data> DictA = new Dictionary<double, Data>();
Dictionary<double, Data> DictB = new Dictionary<double, Data>();
DictA.Add(0, new Data() { DATES = new DateTime(2001, 1, 1), ValueA = 100 });
DictA.Add(1, new Data() { DATES = new DateTime(2001, 2, 1), ValueA = 150 });
DictA.Add(2, new Data() { DATES = new DateTime(2001, 3, 1), ValueA = 400 });
DictB.Add(0, new Data() { DATES = new DateTime(2001, 2, 1), ValueB = 200 });
DictB.Add(1, new Data() { DATES = new DateTime(2001, 3, 1), ValueB = 400 });
DictB.Add(2, new Data() { DATES = new DateTime(2001, 4, 1), ValueB = 300 });
DictB.Add(3, new Data() { DATES = new DateTime(2001, 5, 1), ValueB = 150 });
var tempDictA = DictA.Values.ToDictionary(x => x.DATES, x => x.ValueA);
var tempDictB = DictB.Values.ToDictionary(x => x.DATES, x => x.ValueB);
ObservableCollection<Data> result
= new ObservableCollection<Data>(tempDictB.Select(x => tempDictA.ContainsKey(x.Key)
? new Data() { DATES = x.Key, ValueA = tempDictA[x.Key], ValueB = x.Value }
: new Data() { DATES = x.Key, ValueA = null, ValueB = x.Value })
.Concat(DictA.Where(x => !tempDictB.ContainsKey(x.Value.DATES))
.Select(x => new Data() { DATES = x.Value.DATES, ValueA = x.Value.ValueA, ValueB = null }))
.OrderBy(x => x.DATES));