When a unique key isn’t quite so unique
I've been working on a demo for an upcoming webcast using our Silverlight FlexGrid. I don't know about you, but I'm tired of all the demos with Northwind, so just to make things fresh, I decided to use some tables from the Inland Empire .NET User's Group database. The tables are used in one of the User Group's programs, the Most Valuable Member; "MemberCredit", "Topic", "TopicType", "Member", and "MemberName"
I created an Entity Data Model, and some LINQ to Entity queries to return an IQueryable collection of MemberCreditRecords. Each of these has a Guid associated with the UserId and I decided to use that as the [Key] in the MemberCreditRecords class. Ok, the following LINQ returns 26 records (what I expected)
However, when populating the FlexGrid, only three records would show, each of them being unique by UserId. If I populated the FlexGrid from this code:
I would get the same number of records back, and the FlexGrid would populate. "Hmm." I think, there must be something wrong with my MemberCreditRecord class, and by jove there was.
Thinking a Guid is unique, I had set the [Key] of the class to:
And there was the problem. The server generates the collection with the correct amount of members, but when it serializes the list, it only does the "unique" records, i.e. the field in the class marked as being the key. This is why only three records were being displayed. After talking it over with Bernardo Castilho (he is an uber-smart guy) he came up with this fix by making sure the property with the [Key] attribute will have a unique value.
Bingo it works. By the way, I also tried it with the Microsoft Silverlight DataGrid and got the same results. Just something to keep in the back of your head for future reference. And thanks again to Bernardo.
James
Update. It's not an issue of Guid vs. int. In the collection there are three members. Each member has their own Guid, but there are multiple instances of the same guid in the collection. The serializer checks for "unique" keys so only includes the first. Changing to an int and making that the key is easier to make unique by _staticID ;
J