Michael Friis' Blog

About


Querying relationships with LinqtoCRM

I’ve just recorded a web cast demontrating joins with LinqtoCRM. The piece de resistance is a join across a many-to-many relationship with the intermediary class generated by CRMMetal:

var res = from u in p.Linq()
	join sr in p.Linq() on u.systemuserid.Value equals sr.systemuserid.Value
	join r in p.Linq() on sr.roleid.Value equals r.roleid.Value
        select new { u.fullname, r.name };

The equivalent example query in the CRM SDK is around forty lines, compared to four for LinqtoCRM. Watch the web cast here.

Comments

Guil on

Good stuff 🙂

Do you plan to add support to custom entities?

Cheers,

Reply

friism on

@Guil, custom entities are supported out of the box, the example just uses a standard entity for common frame of reference.

Reply

brenden on

I have an example below of code I have tried to use that is very similar to your demo. It compiles and it authenticates but when the result returns I get a message “Server was unable to process your request”.

Any ideas at all?

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = “QSM”;

CrmService service = new CrmService();

service.Url = “http://qsmcrm:5555/qsm/mscrmservices/2007/CrmService.asmx”;
service.Credentials = new NetworkCredential(“administrator”, “pass@word1”, “crm”);

ICrmService s = new CrmWebService(service);
CrmQueryProvider p = new CrmQueryProvider(s);

var res = from c in p.Linq()
select c.fullname;
foreach (var i in res)
{
Response.Write(i);
}

Reply

friism on

Does the crmqueryprovider work if you don’t use linqtocrm? I.e. can you do a standard query? Otherwise I haven’t a clue.

Reply

Brian on

@brenden
Use the MetadataService, not CrmService
service.Url = “http://qsmcrm:5555/qsm/mscrmservices/2007/MetadataService.asmx”;

Reply

Thomas on

Is there a chance to do something like paging?

I have to retrieve all contacts (about 500 000) and import them in a third party system using the webservice provided.

any idea?

Reply

admin on

@Thomas, yes — there’s even an example here: http://linqtocrm.codeplex.com/wikipage?title=Querying%20CRM%20with%20Linq&referringTitle=Home

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *