I am having an issue with Linq at the moment. We are using the Telerik controls in a WPF application in C#. I have am "array" of clients. The type of array is a QueryableEntityCoreCollectionView, which is from Telerik. This is the source of a gridview.
I now need to make a property which will return a subset of these clients. No matter what conditions I put in my Where clause, the Where fails and gives me the classic Object is not set to an Instance blahblahblah. (the grid is able to get a IEnumerable, so I don't have to convert back into a QueryableEntityCoreCollectionView at the end.
Here is a bit of code I am trying to play around with:
public IEnumerable<Client> FilteredClients {
get {
IEnumerable<Client> res1 = clients.AsEnumerable<Client>();
IEnumerable<Client> res2 = res1.Where(T => 1 == 1);
return res2;
}
}
In the above example, res1 contains 11 elements. res2 does not work. It goes over it, I don't receive an actual exception, but if I put a breakpoint on the return, and check out both res1 and res2, I can see res1 is okay, res2 when I open it's result view, I get the message "Object reference not set to an instance of an object." with the stack trace:
at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray()
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()
Given the search criteria "1==1", I would expect everything to come out of it... But it fails. Even with a real search, like T => T.City == T.City, it also fails the same way. No matter what I give to Where(), it fails. If I simply return res1 here, my full list passes back to my datagrid as an IEnumerable, and everything works fine. As soon as I try to filter on anything, it fails.
Any ideas?
edit: After Hazrelle comment, I tried calling .ToList() ... But everytime I try to do that, I get a Null Exception.
clients.ToList<Client>(); // fails
clients.AsEnumerable<Client>().ToList<Client>(); // fails
Everything I try fails, and I don't know why...
Edit 2: Still not able to filter or anything... However I got the following line of code to pass:
var query = clients.SourceCollection.ToEnumerable().First();
However, query is not a Client, but rather a Telerik.Windows.Data.QueryableProxy , and is still seen as a collection with 11 items. It is NOT just the first Client.
Seems like this QueryableEntityCoreCollectionView object is either bugged, or we are not supposed to play with it, and should only be handled internally by Telerik stuff, but I have resolved the problem by never creating this object, I create a good ol' List of Client directly and give this to the grid instead. Linq works like a charm then...
For some reason, no amount of AsEnumerable, ToList, AsWhatever I did to a QueryableEntityCoreCollectionView(of Client), it always became a QueryableEntityView(of Client) at best, never actually became a simple List(of Client).
Strange.
It is very possible I am loosing the ability to update the database by doing that, However, at this point, this is not data that is meant to be updateable, at least not from that window.