How could I deserialize the queryexpression in the linqpad output?
-
Tuesday, April 03, 2012 2:25 PM
Greetings to all,
In my last thread I had asked how I could convert linq to queryexpression or fetchxml.
I received a hint of using LINQPAD. But after installing and running it. It returns me a serialized version of queryexpression as this but I wanted some thing which I could use directly in the code as FetchXML or some syntax as new QueryExpression(). Could some one please hint me how I could convert this into such a format.
ys,
Sebastian
<QueryExpression xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts"> <ColumnSet> <AllColumns>true</AllColumns> <Columns xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /> </ColumnSet>
- Edited by sebastian.mayer.67 Wednesday, April 04, 2012 9:52 AM
All Replies
-
Tuesday, April 03, 2012 4:01 PM
//The fetch xml query we want to execute string fetchquery = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + "<entity name='account'>" + "<attribute name='name' /> " + "<attribute name='address1_city' /> " + "<attribute name='primarycontactid' /> " + "<attribute name='telephone1' /> " + "<attribute name='accountid' /> " + "<order attribute='name' descending='false' /> " + "<filter type='and'>" + "<condition attribute='statecode' operator='eq' value='0' /> " + "</filter>" + "<link-entity name='contact' from='contactid' to='primarycontactid' visible='false' link-type='outer' alias='accountprimarycontactidcontactcontactid'>" + "<attribute name='emailaddress1' /> " + "</link-entity>" + "</entity>" + "</fetch>"; RetrieveMultipleRequest req = new RetrieveMultipleRequest(); FetchExpression fetch = new FetchExpression(fetchquery); req.Query = fetch; RetrieveMultipleResponse resp = (RetrieveMultipleResponse)service.Execute(req);For more help
Convert Query Expression to fetchXML
Using FetchXML
I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
<hints id="hah_hints"></hints>
Muhammad Mubasher, CRM Developer, The CRM Solutions Pvt Ltd.
My Blogs: http://mubashersharif.blogspot.com
My Linkedin: http://pk.linkedin.com/pub/muhammad-mubasher/34/548/305
My Twitter: https://twitter.com/#!/mubashersharif
- Edited by MubasherSharif Tuesday, April 03, 2012 4:02 PM
- Edited by MubasherSharif Tuesday, April 03, 2012 4:02 PM
- Proposed As Answer by MubasherSharif Tuesday, April 03, 2012 4:05 PM
- Unproposed As Answer by sebastian.mayer.67 Tuesday, April 03, 2012 9:27 PM
-
Tuesday, April 03, 2012 9:30 PM
Thanks Muhammad,
for your response but I do not understand how I could convert LINQPAD output which is a strange serialized format of QueryExpression.
Yes I understand that I could use FetchXML but it is tedious to use the FetchXML generator and easier to write queries in LINQ.
Thus, I was searching for a converter from LINQ to FetchXML or QueryExpression but LINQPAD is not helping me.
ys,
Sebastian
- Edited by sebastian.mayer.67 Wednesday, April 04, 2012 9:53 AM
-
Thursday, April 05, 2012 6:16 AM
Hi Sebastian,
I enjoy a bit of a challenge, so I decided to see what I could get the SDK to do for me in this regard.
NB: I will preface this by stating that it is HIGHLY unsupported and may therefore break at any time in the future.
However, you can convert any CRM LINQ query to its QueryExpression equivalent, and in turn its FetchXml equivalent via the following:
using (var xrm = new XrmServiceContext("Xrm")) { var contacts = ( from c in xrm.ContactSet where c.LastName.StartsWith("B") select new Xrm2011.Contact { FirstName = c.FirstName, LastName = c.LastName, ParentCustomerId = c.ParentCustomerId } ); IQueryProvider queryProvider = contacts.Provider; MethodInfo translateMethodInfo = queryProvider.GetType().GetMethod("Translate"); QueryExpression query = (QueryExpression)translateMethodInfo.Invoke(queryProvider, new object[] { contacts.Expression }); QueryExpressionToFetchXmlRequest reqConvertToFetchXml = new QueryExpressionToFetchXmlRequest { Query = query }; QueryExpressionToFetchXmlResponse respConvertToFetchXml = (QueryExpressionToFetchXmlResponse)xrm.Execute(reqConvertToFetchXml); System.Diagnostics.Debug.Print(respConvertToFetchXml.FetchXml); }Note the use of reflection to invoke the otherwise unavailable Translate method (due to the QueryProvider class being marked as internal). As stated; highly unsupported, but if you're using it merely to accelerate development and not using the technique in production code, I see no harm in it.
--pogo (pat) @ pogo69.wordpress.com
- Marked As Answer by sebastian.mayer.67 Thursday, April 05, 2012 7:29 AM
-
Thursday, April 05, 2012 7:29 AM
Hey Pogo!
This looks really awesome!!! I will write it straigh away! Thanks a million :D
ys,
Sebastian
-
Thursday, April 05, 2012 7:48 AMAnswerer
Hi Pogo/Sebastian,
You might find the following post interesting where I wrote a tool to convert Linq into FetchXml.
http://www.develop1.net/public/post/Convert-CRM2011-Linq-into-a-Query-Expression-and-FetchXml.aspx
Scott
Scott Durow
Read my blog: www.develop1.net/public
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"- Marked As Answer by sebastian.mayer.67 Thursday, April 05, 2012 8:27 AM
-
Thursday, April 05, 2012 8:18 AMYou are a genius Pogo! It works :D
-
Thursday, April 05, 2012 8:29 AM
Hi Scott,
Thanks a lot for! Things are getting better and better!
ys,
Sebastian
-
Wednesday, May 02, 2012 2:16 PM
Hi,
I'm also trying to pull data from CMR, but i have been struggling to put it in a dataset please assist.
Thanx in advance