Accessing a sibling property from within a collection
I have a collection of items that is populated from LINQ and I have 开发者_如何转开发the need to access a sibling item (a date), that will be set from the UI level. Since I do not know the date at the time the data collection is populated, I need a good way to access the date property from each of the items in the collection. Here is my class structure stripped down:
public class TestClass
{
public DateTime MyDate;
public List<MySite_ReportResult> Items { get; set; }
}
To summarize, for each "MySite_ReportResult", I want to be able to access "MyDate". Does anyone have a good suggestion that is a best practice? This collection will eventually be bound to a grid, so I will be creating a partial class and adding an additional property based on what the DateTime value is.
Create a UIDateAndReport class that will wrap a My_SiteReportResult and the Date.
Code goes as follow:
public class TestClass
{
public DateTime Date { get; set; }
public IEnumerable<My_SiteReportResult> Reports {get;set;}
}
public class UIDateAndReport
{
public DateTime Date { get; set; }
public My_SiteReportResult Report { get; set; }
}
TestClass tst = new TestClass();
...
var DatedReports =
(from r in tst.Reports
select new UIDateAndReport { Date = tst.Date, Report = r });
You will end up with an IEnumerable<UIDateAndReport> that you can bind to your UI through an ObservableCollection or a CollectionView.
{enjoy}
Could this help?
...
List<DatedReportResult> list = new List<DatedReportResult>();
foreach (var drs in list) {
drs.ReportDateTime ..
}
...
class DatedReportResult {
public DateTime ReportDateTime;
/* Properties of original ReportResult class */
....
}
Conversely for the UI you could bind the list, use a BindingSource and access the current item's ReportDateTime property in your code.
Hope this helps.
Thanks
精彩评论