"Failed to project selection"

Feb 27, 2009 at 11:31 AM
Hi,

I am running the following LINQ expression to check for any letters in the CRM that have a specific subject and a specific regardingobjectid. Here is my code:

var res = from d in this.query.Linq<letter>()
              where d.subject == version && d.regardingobjectid.Value == id
              select d;

I keep getting the above error. Any idea's to what this means? It has worked fine for a while now this has only started happening.

James.

Coordinator
Feb 28, 2009 at 3:14 PM
Edited Feb 28, 2009 at 7:23 PM
I can't reproduce the error, are you using the latest version of LinqtoCRM?
Developer
Mar 4, 2009 at 10:33 AM
Did you customize the "letter" entity in any way?

I have reproduced the problem. It happens when the result of a query contains a property that the object it is projected to does not. 
This happens when a new field is added to an entity in crm, but the web reference is not updated. Querying goes fine, but when the resulting xml is projected back to the entity, there is a value for a field that does not exist.

I'll upload a patch shortly
Mar 4, 2009 at 10:39 AM
Hi,

friism, yes I am using the latest version of LinqtoCRM.

melg, yes we have added some custom fields to that particular entity..

Regards,
James.
Coordinator
Mar 4, 2009 at 2:36 PM
+1UP to melg -- hadn't seen that one.
Developer
Mar 5, 2009 at 6:58 PM

I found this issue recently as well.  This has the potential to create a lot of issues for anyone using the library who may have customers that modify the entities without the knowledge of the developer.  It is not practical in many parts of application development to specify the attributes to retrieve every time.  I know this is ‘beta’ and should be used in production scenarios with caution but it is a great library and tool and we’ve been using it with great success until this issue surfaced.

I've noticed this issue only with the latest version of the code.  Possible it existed previously and I just got lucky.  Any news on a patch or is there a workaround?

Coordinator
Mar 5, 2009 at 10:49 PM
melg fixed it in the latest commit i believe:
http://linqtocrm.codeplex.com/SourceControl/ListDownloadableCommits.aspx

I'll package up a new release soon.

Developer
Mar 6, 2009 at 3:39 PM

While this helped, found I was still getting this error often.   It turned out to be related to using an orderby in the query.  So this:

var res = (from su in QueryProvider.Linq<systemuser>()
                       where su.new_boolattribute.Value == true
                       select su).ToList();

would work, but this:

var res = (from su in QueryProvider.Linq<systemuser>()
                       where su.new_boolattribute.Value == true
                       orderby su.lastname
                       select su).ToList();

would return the "Failed to Project Selection" error.

As mentioned prevously I've been using the library with great success and have lots of code already in place using this format so trying to track down and change it all was not high on by list of things I wanted to do.  I spent some time stepping through the code and I was eventually able to track down and correct the issue.  It occured because in the projection builder it was only looking for a Generic Type Definition of IQueryable and the ordered by has a type of IOrderedQueryable.  I modified the code at line 39 of the ProjectionBuilder.cs class from

if (t.IsInterface && t.GetGenericTypeDefinition() == typeof(IQueryable<>))

to this

if (t.IsInterface && (t.GetGenericTypeDefinition() == typeof(IQueryable<>))
    || (t.GetGenericTypeDefinition() == typeof(IOrderedQueryable<>))

that seems to have solved the issue.

Coordinator
Mar 7, 2009 at 1:12 PM
Your patch has incorporated in change set 29817, thanks for contributing!