CRM Crate

We will learn and understand the different methods for querying the data in Power Apps using the SDK for .NET. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Power Platform.

Understanding different methods for querying the data in Power Apps using the SDK for .NET

In Power Apps, you can create a Model-Driven App that efficiently accesses the information stored in Micrsoft Dataverse in form of tables & attributes. Tables serve as a means to structure and manage the business data. In the process of app development, one has the option to employ either standard tables, custom tables, or a combination of both. Microsoft Dataverse comes equipped with default standard tables. The attributes are the elements which stores different format of data such as text, integer, date & time, decimal, lookup etc.


Methods for querying data in Power Apps using SDK for .NET

The .NET SDK offers various methods for data querying, each presenting distinct advantages.

The following example demonstrates a simple query to return up to 500 matching CRM Crate Account rows where the ‘usertype’ value equals 1.

var query = new QueryExpression("crmcrateaccount")
{
  ColumnSet = new ColumnSet("name"),
  Criteria = new FilterExpression(LogicalOperator.And),
  TopCount = 500
};
query.Criteria.AddCondition("usertype", ConditionOperator.Equal, 1);
query.AddOrder("name", OrderType.Ascending);

EntityCollection results = svc.RetrieveMultiple(query);

results.Entities.ToList().ForEach(x =>
{
  Console.WriteLine(x.Attributes["name"]);
});

Important Note: When fetching rows, it is recommended to specify the exact column values needed by configuring the attributes through the ColumnSet class constructor. While the ColumnSet class constructor offers an overload with a boolean parameter ‘allColumns’ (Setting the ColumnSet as true), it is recommended not to employ this in production code as it impact application performance.

The following example demonstrates a simple query to return up to 500 matching CRMCrateAccount rows where the “usertype” value equals 1, ordered by name.

var query = new QueryByAttribute("crmcrateaccount")
{
  TopCount = 500,
  ColumnSet = new ColumnSet("name")
};
query.AddAttributeValue("usertype", 1);
query.AddOrder("name", OrderType.Ascending);

EntityCollection results = svc.RetrieveMultiple(query);

results.Entities.ToList().ForEach(x =>
{
  Console.WriteLine(x.Attributes["name"]);
});

The following example shows a simple query to return up to 500 matching account rows where the “usertype” value equals Redmond, ordered by name.

string fetchXml = @"
<fetch top='500' >
  <entity name='crmcrateaccount' >
    <attribute name='name' />
    <filter>
      <condition 
        attribute='usertype' 
        operator='eq' 
        value=1/>
    </filter>
    <order attribute='name' />
  </entity>
</fetch>";

var query = new FetchExpression(fetchXml);

EntityCollection results = svc.RetrieveMultiple(query);

results.Entities.ToList().ForEach(x => {
  Console.WriteLine(x.Attributes["name"]);
});

The provided example demonstrates a comprehensive query operation, encompassing the creation of a data source, formulation of the query expression, and execution of the query within a foreach statement.

////CRM Crate Sample Code For LINQ
// Specify the data source.
int[] values= [95, 90, 83, 65];

// Define the query expression.
IEnumerable<int> crmcrateQuery=
    from value in values
    where value > 90
    select value ;

// Execute the query.
foreach (var i in crmcrateQuery)
{
    Console.Write(i + " ");
}

Important Note: You might need to add a using directive, using System.Linq;, for the preceding example to compile. 


5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.