计数
1 // Fetch the count of all opportunities. This is the equivalent of
2 // SELECT COUNT(*) AS opportunity_count ... in SQL.
3 string opportunity_count = @"
4
5
6
7
8 ";
9
10 EntityCollection opportunity_count_result = _serviceProxy.RetrieveMultiple(new FetchExpression(opportunity_count));
11
12 foreach (var c in opportunity_count_result.Entities)
13 {
14 Int32 aggregate2 = (Int32)((AliasedValue)c["opportunity_count"]).Value;
15 System.Console.WriteLine("Count of all opportunities: " + aggregate2);
16
17 }
18 System.Console.WriteLine("===============================");
最大值
1 // Fetch the maximum estimatedvalue of all opportunities. This is the equivalent of
2 // SELECT MAX(estimatedvalue) AS estimatedvalue_max ... in SQL.
3 string estimatedvalue_max = @"
4
5
6
7
8 ";
9
10 EntityCollection estimatedvalue_max_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_max));
11
12 foreach (var c in estimatedvalue_max_result.Entities)
13 {
14 decimal aggregate5 = ((Money)((AliasedValue)c["estimatedvalue_max"]).Value).Value;
15 System.Console.WriteLine("Max estimated value of all opportunities: " + aggregate5);
16
17 }
18 System.Console.WriteLine("===============================");
总和
// Fetch the sum of estimatedvalue for all opportunities. This is the equivalent of
// SELECT SUM(estimatedvalue) AS estimatedvalue_sum ... in SQL.
string estimatedvalue_sum = @"
";
EntityCollection estimatedvalue_sum_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_sum));
foreach (var c in estimatedvalue_sum_result.Entities)
{
decimal aggregate7 = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate7);
}
System.Console.WriteLine("===============================");
多个聚合
// Fetch multiple aggregate values within a single query.
string estimatedvalue_avg2 = @"
";
EntityCollection estimatedvalue_avg2_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_avg2));
foreach (var c in estimatedvalue_avg2_result.Entities)
{
Int32 aggregate8a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
System.Console.WriteLine("Count of all opportunities: " + aggregate8a);
decimal aggregate8b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate8b);
decimal aggregate8c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate8c);
}
System.Console.WriteLine("===============================");
分组依据
// Fetch a list of users with a count of all the opportunities they own using groupby.
string groupby1 = @"
";
EntityCollection groupby1_result = _serviceProxy.RetrieveMultiple(new FetchExpression(groupby1));
foreach (var c in groupby1_result.Entities)
{
Int32 aggregate9a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
System.Console.WriteLine("Count of all opportunities: " + aggregate9a + "\n");
string aggregate9b = ((EntityReference)((AliasedValue)c["ownerid"]).Value).Name;
System.Console.WriteLine("Owner: " + aggregate9b);
string aggregate9c = (string)((AliasedValue)c["ownerid_owneridyominame"]).Value;
System.Console.WriteLine("Owner: " + aggregate9c);
string aggregate9d = (string)((AliasedValue)c["ownerid_owneridyominame"]).Value;
System.Console.WriteLine("Owner: " + aggregate9d);
}
System.Console.WriteLine("===============================");
按链接的实体进行分组
// Fetch the number of opportunities each manager's direct reports
// own using a groupby within a link-entity.
string groupby2 = @"
";
EntityCollection groupby2_result = _serviceProxy.RetrieveMultiple(new FetchExpression(groupby2));
foreach (var c in groupby2_result.Entities)
{
int? aggregate10a = (int?)((AliasedValue)c["opportunity_count"]).Value;
System.Console.WriteLine("Count of all opportunities: " + aggregate10a + "\n");
}
System.Console.WriteLine("===============================");
按年分组
// Fetch aggregate information about the opportunities that have
// been won by year.
string byyear = @"
";
EntityCollection byyear_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byyear));
foreach (var c in byyear_result.Entities)
{
Int32 aggregate11 = (Int32)((AliasedValue)c["year"]).Value;
System.Console.WriteLine("Year: " + aggregate11);
Int32 aggregate11a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
System.Console.WriteLine("Count of all opportunities: " + aggregate11a);
decimal aggregate11b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate11b);
decimal aggregate11c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate11c);
System.Console.WriteLine("----------------------------------------------");
}
System.Console.WriteLine("===============================");
多个分组依据
// Fetch aggregate information about the opportunities that have
// been won by year and quarter.
string byyrqtr = @"
";
EntityCollection byyrqtr_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byyrqtr));
foreach (var c in byyrqtr_result.Entities)
{
Int32 aggregate16d = (Int32)((AliasedValue)c["year"]).Value;
System.Console.WriteLine("Year: " + aggregate16d);
Int32 aggregate16 = (Int32)((AliasedValue)c["quarter"]).Value;
System.Console.WriteLine("Quarter: " + aggregate16);
Int32 aggregate16a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
System.Console.WriteLine("Count of all opportunities: " + aggregate16a);
decimal aggregate16b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate16b);
decimal aggregate16c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate16c);
System.Console.WriteLine("----------------------------------------------");
}
System.Console.WriteLine("===============================");
排序依据
// *****************************************************************************************************************
// Specify the result order for the previous sample. Order by year, then quarter.
string byyrqtr2 = @"
";
EntityCollection byyrqtr2_result = _serviceProxy.RetrieveMultiple(new FetchExpression(byyrqtr2));
foreach (var c in byyrqtr2_result.Entities)
{
Int32 aggregate17 = (Int32)((AliasedValue)c["quarter"]).Value;
System.Console.WriteLine("Quarter: " + aggregate17);
Int32 aggregate17d = (Int32)((AliasedValue)c["year"]).Value;
System.Console.WriteLine("Year: " + aggregate17d);
Int32 aggregate17a = (Int32)((AliasedValue)c["opportunity_count"]).Value;
System.Console.WriteLine("Count of all opportunities: " + aggregate17a);
decimal aggregate17b = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value;
System.Console.WriteLine("Sum of estimated value of all opportunities: " + aggregate17b);
decimal aggregate17c = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
System.Console.WriteLine("Average of estimated value of all opportunities: " + aggregate17c);
System.Console.WriteLine("----------------------------------------------");
}
System.Console.WriteLine("===============================");