I’m writing a collection of anonymous type into WPF DataGrid.ItemsSource. And now I want it back. How do I do this, Is it possible?
How can I reconstruct the anonymous type?
Thanks!
EDIT
Ok. So could You say will this work?
if(this.AbcDataGrid.ItemsSource != null && this.XyzDataGrid.ItemsSource != null)
{
var abcdata = (IEnumerable<dynamic>)this.AbcDataGrid.ItemsSource;
var xyzdata = (IEnumerable<dynamic>)this.XyzDataGrid.ItemsSource;
var d1 = abcdata.ToList().OrderByDescending(x => x.Id);
var d2 = xyzdata.ToList().OrderByDescending(x => x.Id);
var result = from i1 in d1
from i2 in d2
select new
{
Name = i1.Name,
Group = i1.Group.ToString() + i2.Group.ToString()
};
}
Properties Group and Name both present in anonymous type declaration.
There are several options, but the best one is: don’t do it. Anonymous types are meant to be used only in one function, not to store them somewhere and then retrieve them. Just create a normal class.
If you are sure you want to use anonymous type, you could use cast by example:
Or you could use dynamic:
Note that you could cast to
dynamic[], but doing so is not safe. Another option would be to cast to justdynamic, leaving type-safety completely.