0
votes

Im a newcomer to CRM 2011 plugins. Here, I is a piece of my code which throws error:

private static void AddWRItoServiceActivity(IOrganizationService service, Guid id)
    {
        using (var crm = new XrmServiceContext(service))
        {

            var serviceactivity = crm.ServiceAppointmentSet.Where(c => c.Id == id).First();
            var serviceitem = crm.brd_serviceitemSet.Where( c => c.brd_RegardingServiceId == serviceactivity.ServiceId);

            for (int i = 1; i < serviceitem.Count(); i++)
            {
                var workReportItem = new brd_workreportitem
                   {
                       brd_name = "By payman Plugin",
                       brd_serviceappointment_brd_workreportitem = serviceactivity,
                   };
                crm.AddObject(workReportItem);
                crm.SaveChanges();
            }
        }
    }

I've tried this:

for (int i = 1; i < serviceitem.ToList().Count(); i++)

and this gives error also. Would you please help me with counting such as this or using other syntax such as foreach? P.S.: I've tried this also:

foreach (var s in serviceitem.ToList())
4
The method 'Count' is not supported. - Payman Biukaghazadeh
Have you tried this? foreach(var s in serviceitem) - Charan Raju C R
offcoure! this is the error when using ur suggestion: The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'CrmEntityReference' and namespace 'Microsoft.Xrm.Client'.Detail: - Payman Biukaghazadeh
Is XrmServiceContext your early bound classes? - James Wood
yes. XrmServiceContext is early bound classes. - Payman Biukaghazadeh

4 Answers

1
votes

As @Anwar suggests, the Linq to CRM expressions eventually get converted into QueryExpressions, which do not support summation or aggregate expressions. I'm guessing the Linq Count method is trying to get converted into a Count Queryexpression which will fail. Now why the serviceitem.ToList().Count() doesn't work, I'm more confused. The ToList should cause the fetching of the entities, which is then added to a list, and then your standard LINQ Count method is used... You do have a using statement for LINQ correct?

Why don't you use a foreach(var item in serviceitem)?

EDIT: Didn't see the comment.

Based on the error in your oomment, it looks like you haven't enabled proxy types on your IOrganizationService, or are not including your custom entities on the server.

0
votes

I cant see anything immediately wrong with that code, it may be how you have setup your project and early bound classes.

I would suggest having a read of Using LINQ in CRM 2011 Plugins, try following the steps outlined there, because he is describing what you want to achieve.

0
votes

FetchXML is the only thing which supports the Count and Sum functionalities. Count and Sum are considered to be an Aggregation. Unfortunately, aggregation doesn't work in Linq as you mentioned.

Here is an explanation of using FetchXML.

0
votes

Finally I succeed to solve it. Comparison of Ids helped foreach to run.

var serviceitem = crm.brd_serviceitemSet.Where( c => c.brd_RegardingServiceId.Id == serviceactivity.ServiceId.Id);