locked
Plugin Error When doing Count RRS feed

  • Question

  •  var complexQuery = (from c in mycontext.CreateQuery<new_contractorsframework>()
                                                join e in mycontext.CreateQuery<new_eval>()                                             
                                                 on c.new_ContractorCompanyName equals e.RegardingObjectId.ToString()
                                                 
                                                where e.new_QW1 != null
                                                select e.new_QW1).Count();
                            
                            if (complexQuery == null)
                            {
                                var newQuery = "Blank";
                                entity.Attributes.Add("new_testcomplexquery", newQuery);
                            }
                            else
                            {
                                var newQueryToo = "HasData";
                                entity.Attributes.Add("new_testcomplexquery", newQueryToo);
                                
    
                            }

    I have a plugin and I keep getting the following error message when I run it:

    <Message>Unexpected exception from plug-in (Execute): Testing.PreEvaluationCreate: System.NotSupportedException: Invalid &amp;#39;join&amp;#39; condition. An entity member is invoking an invalid property or method.</Message>

    This error message only appears when I add the .Count() method to the code.

    If I dont have the .Count() at the end of the select it works fine but i need a Count of the number of records returned to complexQuery.

    Anyone know what the problem is?



    • Edited by JMcCon Friday, October 23, 2015 11:38 AM
    Friday, October 23, 2015 11:21 AM

All replies

  • I want to use Linq in a plugin to do a count of records in an entity.

    Anyone any code to achieve this?

    Friday, October 23, 2015 3:14 PM
  • You can use early or late bind to achieve the requirement.

    var count = (from a in Service.CreateQuery("account")
    select a).Count();

    OR 
    var count  = (from a in context.AccountSet
                           where a.fieldNme== value
                           select a).ToList().Count();

    Monday, October 26, 2015 3:23 PM
  • try to convert to List first.

    var complexQuery = (from c in mycontext.CreateQuery<new_contractorsframework>()
                                                join e in mycontext.CreateQuery<new_eval>()                                             
                                                 on c.new_ContractorCompanyName equals e.RegardingObjectId.ToString()          
                                                where e.new_QW1 != null
                                                select e.new_QW1).ToList().Count();

    Monday, October 26, 2015 3:36 PM
  • Thanks for the reply Kalim

    I have the following written and I want to see the value of complexQuery to use in an If statement

    int complexQuery = (from e in mycontext.CreateQuery<new_eval>()
                                                
                                                where e.new_QW1 != null
                                                select e.new_QW1).Count();
    
                            
    
                            
                           if (complexQuery < 1)
                            {
                                var newQuery = "HasData";
                                entity.Attributes.Add("new_testcomplexquery", newQuery);
                            }
                            else
                            {
                                var newQueryToo = "Blank";
                                entity.Attributes.Add("new_testcomplexquery", newQueryToo);
                                
    
                            }

    It doesnt seem to pick up the value of complexQuery as being less than 1.

    Is there anyway I could see what the value of complexQuery is?

    Tuesday, October 27, 2015 12:43 PM
  • I am using CRM 2011. Is the Count() method supported in crm 2011. If it is not how do you do a Count in CRM 2011 Plugin?

    I have the following written and I want to see the value of complexQuery to use in an If statement.

    int complexQuery = (from e in mycontext.CreateQuery<new_eval>()
                                                
                                                where e.new_QW1 != null
                                                select e.new_QW1).Count();
    
                            
    
                            
                           if (complexQuery < 1)
                            {
                                var newQuery = "HasData";
                                entity.Attributes.Add("new_testcomplexquery", newQuery);
                            }
                            else
                            {
                                var newQueryToo = "Blank";
                                entity.Attributes.Add("new_testcomplexquery", newQueryToo);
                                
    
                            }

    It doesnt seem to pick up the value of complexQuery as being less than 1.

    Is there anyway I could see what the value of complexQuery is?



    Wednesday, October 28, 2015 9:24 AM
  • Please, don't create duplicated threads.

    Dynamics CRM MVP
    My blog

    Thursday, October 29, 2015 6:17 AM
    Moderator