Is there a way to view LINQ Generated query expressions in CRM Dynamics? - linq

Considering the fact LINQ queries in CRM Dynamics are translated into query expressions (source):
[...] The OrganizationServiceContext class contains an underlying LINQ
query provider that translates LINQ queries from Microsoft Visual C#
or Microsoft Visual Basic .NET syntax into the query API used by
Microsoft Dynamics CRM. [...]
Is there a way to see the generated query expressions (As it's possible to see the generated SQL query in Linq-to-Sql or Linq-to-Entities)?

You can use reflection to get the query object and then convert that to a FetchXML query to get a printable query. This will work with both early-bound and late-bound queries.
From: https://pogo69.wordpress.com/2012/04/05/crm-linq-provider-converting-expressions-to-queryexpression-andor-fetchxml/
var connectionString = #"SET YOUR CONNECTION STRING";
var service = new CrmServiceClient(connectionString);
using (var xrm = service.OrganizationServiceProxy)
{
OrganizationServiceContext orgContext =
new OrganizationServiceContext(xrm);
var query = from c in orgContext.CreateQuery("contact")
join a in orgContext.CreateQuery("account")
on c["contactid"] equals a["primarycontactid"]
where (String)c["lastname"] == "Wilcox" ||
(String)c["lastname"] == "Andrews"
where ((String)a["address1_telephone1"]).Contains("(206)")
|| ((String)a["address1_telephone1"]).Contains("(425)")
select new
{
Contact = new
{
FirstName = c["firstname"],
LastName = c["lastname"]
},
Account = new
{
Address1_Telephone1 = a["address1_telephone1"]
}
};
IQueryProvider queryProvider = query.Provider;
MethodInfo translateMethodInfo = queryProvider.GetType().GetMethod("Translate");
QueryExpression queryEx = (QueryExpression)translateMethodInfo.Invoke(queryProvider, new object[] { query.Expression });
QueryExpressionToFetchXmlRequest reqConvertToFetchXml = new QueryExpressionToFetchXmlRequest { Query = queryEx };
QueryExpressionToFetchXmlResponse respConvertToFetchXml = (QueryExpressionToFetchXmlResponse)xrm.Execute(reqConvertToFetchXml);
Console.WriteLine("To FetchXML:" + Environment.NewLine + Environment.NewLine);
Console.WriteLine(respConvertToFetchXml.FetchXml);
Alternatively you could use Fiddler to capture the actual query text sent in the SOAP message. I've done this before and haven't found it any more valuable than the FetchXml.

Related

Get to underlying SQL for Cosmos DB Query generated via LINQ

I am creating a query to cosmos using Linq
This is converted into SQL which is then run to do the search
var modelName = "Mondeo";
var baseQuery = client.CreateDocumentQuery<Car>(StaticSettings.ProjectionsCollectionUri,
new FeedOptions { MaxItemCount = maxItemCount, PartitionKey = new PartitionKey(partitionKey) })
.Where(order => car.ModelName == modelName);
If I run this code and put a breakpoint after this statement, I can see the raw SQL query generated
This is shown in the first line of the inspector
{{"query":"SQL HERE"}}
How can I get to this via code?
I am looking to get to this SQL to ensure that it is what I want it to be and I can use it in my tests
Paul
Assuming you're using Visual Studio, the debugger by default would be showing you the output of ToString() in the inspector for any given object.
With that knowledge, you could retrieve that same query string object with the following code.
var serializedQuery = baseQuery.ToString(); // "{{\"query\":\"SQL HERE\"}}"
The result appears to be a serialized JSON object that wraps the actual SQL query. You can easily extract the SQL with Newtonsoft.Json using the following code.
var sql = JObject.Parse(serializedQuery)["query"].Value<string>(); \\ "SQL HERE"
EDIT: In current versions of the SDK, the solution would look like the following instead.
var baseQuery = container
.GetItemLinqQueryable<Car>(
requestOptions: new QueryRequestOptions
{
MaxItemCount = maxItemCount,
PartitionKey = new PartitionKey(partitionKey)
}
)
.Where(order => car.ModelName == modelName);
var sql = baseQuery.ToQueryDefinition().QueryText; // "SQL HERE"

CRM Linq find all parents that have 0 children

How can I find (preferably using CRM Linq) parent entities that have 0 children. For example how can I find all accounts that have 0 contacts.
If you are going to use the query expression route, which I would recommend then the following code will be useful
var entityAlias = "con";
var query = new QueryExpression
{
EntityName = "account",
ColumnSet = new ColumnSet(true),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression(entityAlias, "contactid",ConditionOperator.Null)
}
}
LinkEntities =
{
new LinkEntity
{
EntityAlias = entityAlias,
LinkFromEntityName = "account",
LinkFromAttributeName = "accountid",
LinkToEntityName = "contact",
LinkToAttributeName = "parentcustomerid",
Columns = new ColumnSet("parentcustomerid", "contactid"),
JoinOperator = JoinOperator.LeftOuter,
}
},
};
var response = service.RetrieveMultiple(query);
var accounts = response.Entities;
In this code I have not limited the columns, this will reduce performance and you should only return the columns needed.
If there is the case for more than 5000 records are going to be returned then you will need to use paging and loop the query to find all the entities,
This can be found here:
https://msdn.microsoft.com/en-us/library/gg327917.aspx
However if you are certain you want to use LINQ then you can use the following code:
public static IEnumerable<Account> FindAccountsWithNoContacts()
{
var contactRelationship = new Relationship("contact_customer_accounts");
foreach(var account in XrmContext.AccountSet)
{
XrmContext.LoadProperty(contactRelationship);
if(!account.RelatedEntities.ContainsKey(contactRelationship)
yield return account;
}
}
My problem with the LINQ code is that all the enities, both the account and contact entities, will be loaded into memory. With large entity sets this can cause OutOfMemoryException, whereas the query expression route will pass the query to the Dynamics server to execute; which should make the execution of the code faster.
The thing you are looking for is left outer join. Which is unfortunately not possible in CRM using LINQ. However you can do it using query expression or FetchXML.
Here is a link that can help you:
https://community.dynamics.com/crm/b/gonzaloruiz/archive/2014/02/23/all-about-outer-join-queries-in-crm-2011-and-crm-2013

How to use a string in the linq where clause?

I am trying to send a Linq query as a string to a method to be used in a where clause. Since IEnumerable wouldn't work for this, I have converted my IEnumerable to IQueryable and still it throws error. The following is the code:
public static void FilterData(string Query)
{
if((List<MemberMaintenanceData>)HttpContext.Current.Session["Allmembers"] != null)
{
//Get the IEnumerable object colection from session
var data = (List<MemberMaintenanceData>) HttpContext.Current.Session["Allmembers"];
//Convert it to IQueryable
IQueryable<MemberMaintenanceData> queryData = data.AsQueryable();
//This line doesn't compile!!
queryData = queryData.Where(Query);
HttpContext.Current.Session["Allmembers"] = queryData.AsEnumerable().ToList();
}
}
I intended passing "a => a.AccountId == 1000" as Query
There is a free (and open source) library, provided by Microsoft for parsing strings into Lambda expressions that can then be used in Linq queries. It also contains versions of the standard query operators such as Where() that take a string parameter. You can find it described in Scott Guthries blog post on Dynamic Linq.
For example, you can do queries like this (adapted from a snippet from the Scott guthrie link)
// imagine these have come from a drop down box or some other user input...
string thingToSelectBy = "City";
string citySelectedByUser = "London";
int minNumberOfOrders = 10;
string whereClause = String.Format("{0} = #0 and Orders.Count >= #1", thingToSelectBy);
var query = db.Customers
.Where(whereClause, citySelectedByUser, minNumberOfOrders)
.OrderBy("CompanyName")
.Select("new(CompanyName as Name, Phone");
The Where clause in thisw code snippet shows how you create a where clause using a parameterised string and then dynamically inject values for the parameters at run time, for example, based on user input. This works for parameters of any type.
In your example, the where clause would be
whereClause = "AccountId = 1000";
So in effect you would be doing something like
var newFilteredQueryData = queryData.Where("AccountId = 1000");
That link also contains the location where you can download the source code and a comprehensive document describing the dynamic query API and expression language.
Given a class such as:
public class foo
{
public int AccountID {get;set;}
}
You should be able to do something like this:
Expression<Func<foo, bool>> filter = f => f.AccountID == 1000;
And then pass that as your query. If it is really needed as a string you can do this:
filter.ToString();
//By Using this library
using System.Linq.Dynamic.Core;
InventoryList = Repository.GetAll(); // IQueryable
string filterString = "UnitPrice > 10 And Qty>100 OR Description.Contains("Dairy")";
var filteredGenericList = InventoryList.Where(filterString);

SharePoint Web Services Query

Consider the following (working) query. It returns an entire SharePoint row, then I get the value I (really wanted) from the row. How can I return only that value?
var ctx = new webservc.MyDataContext(new Uri("http://uri"));
ctx.Credentials = CredentialCache.DefaultCredentials;
var query = from c in ctx.MyList orderby c.ValIWant descending select c;
foreach (webservc.MyListItem item in query.Take(1))
{
return (int)item.ValIWant;
}
return 0;
I tried doing "select c.ValIWant" but I get an exception as a result. I cannot use LINQ stuff such as ".Max()" because SharePoint does not support it in the WCF Web Services, so I'm trying to stick to simple queries like the one I have above.

NHibernate LINQ 3.0 Oracle Expression type 10005 is not supported by this SelectClauseVisitor

I have the following LINQ query
QueryResult<List<PersonDemographic>> members = new QueryResult<List<PersonDemographic>>();
var query = (from ms in this.Session.Query<MemberSummary>()
where ms.NameSearch.StartsWith(firstName.ToUpper())
&& ms.NameSearch2.StartsWith(lastName.ToUpper())
select new PersonDemographic
{
FirstName = ms.FirstName.ToProperCase(),
LastName = ms.LastName.ToProperCase(),
PersonId = ms.Id,
Address = new Address
{
Line1 = ms.AddressLine1.ToProperCase(),
Line2 = ms.AddressLine2.ToProperCase(),
City = ms.City.ToProperCase(),
State = ms.State,
Zipcode = ms.Zipcode,
},
PhoneNumber = new PhoneNumber
{
Number = string.IsNullOrWhiteSpace(ms.PhoneNumber) ? null : Regex.Replace(ms.PhoneNumber, #"(\d{3})(\d{3})(\d{4})", "$1-$2-$3")
}
});
if (this.Session.Transaction.IsActive)
{
members.Data = query.Distinct().Take(15).ToList();
}
else
{
using (var transaction = this.Session.BeginTransaction())
{
members.Data = query.Distinct().Take(15).ToList();
transaction.Commit();
}
}
The code is running under the transaction section. If I use it without a Distinct I have no problem. Adding the Distinct gives me an exception
{"Expression type 10005 is not supported by this SelectClauseVisitor."}
I can't find anything definitive. Can anyone help?
Thanks,
Paul
There are lots of things in that query that NH can't possibly know how to translate to SQL:
ToProperCase (that looks like an extension method of yours)
string.IsNullOrWhiteSpace (that's new in .NET 4, NH is compiled against 3.5)
Regex.Replace (that's just not possible to do with SQL, unless you have a DB that supports it and write a Dialect for it)

Resources