Query Expression in Dynamics 365

Hello Guys,

In my previous blog i have shown you how you can make connection between D365 and C# Client. Today i'm going to show you how you can retrieve data from Dynamics. But before if you are new to our blog then do follow us to know more about Dynamics and Power Platform. Also if you like our blog then please comment and share this blog with your friends.

While working with C# client we may have come across such scenario where we need to retrieve data from the Dynamics and need to display those data either in console screen or in grid of windows application or in a web api.

To retrieve data from the dyanamics in c# application we are using a class known as QueryExpression which is present in Microsoft.Xrm.Sdk.Query namespace. 

To know more about Query Expression click here.

In this blog i will show you how you can write various query expression just like we were writing a query in Structure Query Language. To show demonstration i have made three entities in Dynamics CRM which are as below:
  • Employee 
  • City
  • Department

We will be working with our previous project that is D365Connector which we have created in visual Studio 2017. We have made one method named GetEmployee in CRMHelper.cs file. This method will give us data which we are going to retrieve using Query Expression. So we will be wrting various queries here.
    
1.Display all records of Employee.


Here in above example,
    
EntiyName :- Schema name of the entity.
ColumnSet :- It indicates which columns you want to display. If you want to    display all which is similar to (*) in Sql then you have to use boolean value true.
NoLock       :- Indicates that no database locks are issued against the data that would prohibit other transactions from modifying the data in the records returned from the query.

we will call this method and write the code for displaying data fetched through the query.


2. Display all records of Employee along with selected columns.


3. Display all records of Employee whose Lastname is "Gupta"


4. Display all records of the employees whose salary is greater than 25000.


5. Display all records of the employees whose salary is less than 25000 or equal to 25000.


6. Display records of employees whose first name contains only four letters.


7. Display records of employees whose first name starts with "A".


8. Display records of employees whose first name starts with "A","H", or "R".


9. Display records of employees whose first name does not starts with "A","H" or "R".


10. Display records of employees whose salary is between 30000,50000.


11. Display records of employees whose last name is either "Gupta" or "Kaur".


12. Display records of employees whose last name is "Gupta" and Salary is greater than 40000.


13. Display all records of the employee in ascending order of the first name.


14. Sort employees by last name and then by their salary.


For displaying salary we need to use Money class casting.



15. Display records of employee along with their department and department location.

Here we need to join employee entity with department entity and to do so we will be using Link Entities.



16. Display all records of employee who belongs to "IT" department.


17. Display records of the employee along with their departments. If employee does not belong to any department then display blank in department.

here we need to use left join to achieve this.


In this way you can use Query Expression to get data from the Dynamics 365. In our upcoming blog i will show how you can retrieve data using paging concept in D365.

Hope it helps...







Comments

Post a Comment

Popular posts from this blog

Read Only Sub Grid & Editable Sub grid in Dynamics 365

Hiding Sub Grid buttons using Ribbon Workbench and JavaScript in Dynamics 365.

Creating, Modifying and Copying Security Roles in Dynamics 365.