Using LINQ to retrieve all invoices with customerid.type = contact

Feb 20, 2009 at 2:15 PM

I am trying to perform a query using LINQ expressions that will retrieve all invoices who's customer field is of type contact. I have tried the following to no avail:

Code Sample
    var results =  from inv in this.query.Linq<invoice>()
                        where inv.customerid.type ==
                        select inv;

I previously built the queries up using QueryExpression however I changed to LINQ as it is far more easier to use and reduced my code by around 50%. Is it possible to perform the above query using LINQ? or am I looking at going back to the lengthy old way for this one particular scenario?

Feb 23, 2009 at 8:46 AM
(sorry I took so long to answer, I've been on holiday)

Yeah, I can see how this doesn't work. I'm not sure how you would express this using queryexpression or fetchxml (which LinqtoCRM uses), or even using "advanced find" in CRM. If you can show how to this, I could probably get LinqtoCRM to copy it.

Feb 23, 2009 at 8:51 AM
Hi Michael,

Yes I have done this using the Advanced find I will post the FetchXML for you to view.

I can see why the above query doesnt work also as the value of "inv.customerid.type" is the index of "Customer" in the enumeration "AttributeType".

Feb 23, 2009 at 8:59 AM

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">

    <entity name="invoice">
<attribute name="name"/>

        <attribute name="totalamount"/>

        <attribute name="customerid"/>

        <attribute name="statuscode"/>

        <attribute name="createdon"/>

        <attribute name="invoiceid"/>

        <order attribute="name" descending="false"/>
<link-entity name="contact" from="contactid" to="customerid" alias="invoicecustomeridcontactcontactid">
            <attribute name="emailaddress1"/>
<filter type="and">
                <condition attribute="contactid" operator="not-null"/>

That is the XML that was generated by the CRM when I did the query in the Advanced Find.


Feb 24, 2009 at 7:07 AM
Right, you can explicitly join to the entitytype you want:
from i in p.Linq<invoice>()
join a in p.Linq<account>() on i.customerid.Value equals a.accountid.Value
select i;
Let me know if it isn't working for you.

Feb 24, 2009 at 9:11 AM
Hi Michael,

Thanks your code works pefect!