This project has moved. For the latest updates, please go here.

EntityObservableCollection

Mar 12, 2010 at 7:08 PM

Nice job on the framework--very impressive.

Per your recommendation on StackOverflow, I have been taking a look at the EntityObservableCollection<T> class. The method overrides are straightforward enough, but I am a little puzzled by the constructor. I can see that it is passing the items list to the base ObservableCollection<T> class, but I am unfamiliar with the syntax it is using to do that:

base(items ?? new T[] {})

It's the latter part that I am puzzling over, the "?? new T[] {}". What does that do, and where do I learn about it?

One other question: I am assuming that the items in the list passed in to the constructor don't need to be added to the ObjectContext, since the list was generated by the EF. Presumably, they were added to the ObjectContext at that time. Is this correct?

Thanks again for your help.

Mar 12, 2010 at 11:05 PM
Edited Mar 12, 2010 at 11:05 PM

Found my answer--it's a LINQ expression, of course. As I am just getting started with LINQ (as well as EF4) I didn't recognize it. If you could provide a brief explanation of what the LINQ expression is doing, it would be a great help to me. Thanks!

Mar 13, 2010 at 9:52 AM
Edited Mar 13, 2010 at 9:54 AM

The ?? operator is a standard C# operator, not specifically part of LINQ. It allows you to provide a default value if the operand you are checking (items in this case) is null.

- If items != null then it will call base(items).
- If items == null then it will call base(new T[]{}).

The first two code snippets here show how it works.

Mar 13, 2010 at 1:23 PM

No kidding! I hadn't come across that operator before. Very helpful--thanks. That pretty much explains the new constraint, then.