This project is read-only.

Generic Methods using Toolkit

Jul 19, 2010 at 1:13 PM

Hi,

I am trying to create a generic method to retrieve any entity from crm using the following:

public static List<T> GetAll<T>()
        {
            return GetAll<T>(null, null);
        }

        public static List<T> GetAll<T>(QueryExpression qe, CrmService crmService)
        {
            IEnumerable<T> entities = crmService.RetrieveMultiple<T>(qe);

            return entities.ToList<T>();
        }

 

But I get the following errors:

  • The type 'T' cannot be used as type parameter 'TEntity' in the generic type or method 'Streamsol.Crm.CrmServiceHelper.RetrieveMultiple<TEntity>(Microsoft.Crm.SdkTypeProxy.CrmService, Microsoft.Crm.Sdk.Query.QueryBase)'. There is no boxing conversion or type parameter conversion from 'T' to 'Streamsol.Crm.XrmDevelopmentToolkit.IXrmEntity'.
  • 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TEntity' in the generic type or method 'Streamsol.Crm.CrmServiceHelper.RetrieveMultiple<TEntity>(Microsoft.Crm.SdkTypeProxy.CrmService, Microsoft.Crm.Sdk.Query.QueryBase)'

Is it possible to do with your toolkit?

 

Thanks!

 

Jul 21, 2010 at 5:34 PM
Edited Jul 21, 2010 at 5:39 PM
Yes, You will have to add a 'where' clause to T, see below (I have also :

public static List<T> GetAll<T>() where T : IXrmEntity, new()
{
return GetAll<T>(null, null);
}

public static List<T> GetAll<T>(QueryExpression qe, CrmService crmService) where T : IXrmEntity, new()
{
IEnumerable<T> entities = crmService.RetrieveMultiple<T>(qe);

return entities.ToList();
}
Jul 22, 2010 at 1:16 PM

Thank you so much kgooding.

It worked like a charm.