Duplicate items when consuming ODATA service
.net-4.0, c#, linq, wcf-data-services
Solution
This usually indicates that you're having a view with key columns (in the EF model) that are not unique. EF just repeats the first row it has read with a set of key values.
The difference with and without `ToList()` is unexpected though. It's just a way to enumerate the query, which Linqpad also does when executing `Dump()`.
Problem
The WCF/OData webservice returns restful items of type `vBeheer`. On client I created the method that consumes: ``` SRTDBEntities es = new SRTDBEntities(_serviceRoot); return es.vBeheer.Where(b => b.TrackID == trackid && b.PersoonID == CurrentPersoon.ID ); ``` However I noticed (after an afternoon of debugging) When adding .ToList() to the expression, the resultset goes corrupted. The `Count()` of the items is correct and as expected, but the values of the items are wrong. Many are duplicate and many are missing (obviously). So I tested this in linqpad: ``` vBeheer.Where (b => b.TrackID==23 && b.PersoonID == 19 ).Dump(); ``` - returns correct results fom the service as expected, note that ID should be an unique value per "row": but when adding .ToList() it goes wrong ``` vBeheer.Where (b => b.TrackID==23 && b.PersoonID == 19 ).ToList().Dump(); ``` this results many duplicate (without any pattern?) items This behaviour is unexpected and unwanted. And even more interesting. We determined that the database returns correct results, on debugging the server (in the webservice) the results are still correct, but when arriving on the client, it goes horribly wrong. IT wouldn't be a problem, but the data is used by a DevExpress Component (DXGrid) that does a ToList() anyway. nb: executing the query in the browser: ``` http://localhost:52671/SRTDataService.svc/vBeheer()?$filter=TrackID%20eq%2023%20and%20PersoonID%20eq%2019 ``` returns an expected set of unique items.