1

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.

2 Answers 2

1

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}

1
  • I ended up having a lot of other complications with typing and the fact that this class was actually using generics (maybe I shouldn't have simplified it so much) and was used in many places, but your solution was helpful in getting toward my resolution so thank you very much. Aug 14, 2010 at 21:49
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.