Appframe Knowledge Base


1 hits
0

How to get and use data from a data source in Razor

// case sensitive stuff!

@{ var data = GetRows("dsMyDatasource", "MyField = 'Something'"); }

@data.Count <!-- Returns the number of rows contained in 'data' -->
@data.Sum("FieldName") <!-- Return the sum of all the values in the specified column -->
@data.Min("FieldName") <!-- Return the smallest of all the values in the specified column -->
@data.Max("FieldName") <!-- Return the largest of all the values in the specified column -->
@data.Average("FieldName") <!-- Return the average of all the values in the specified column -->

@foreach(dynamic row in table){
    <!-- The dynamic keyword makes it possible to access fields on the row 
         without using square brackets ( ["FieldName"] ) -->

    <!-- Return the typed value of a field -->
    @row["FieldName"] 
    @row.FieldName
    
    <!-- Write the value of a field using the default format or using a specified format string -->
    @row.Write("FieldName")
    @row.Write("FieldName", "dd-MMM-yyyy")
}

<!-- Group the data by one or more fields by using .GroupBy(ParamArray pFieldName() As String) -->
@foreach(dynamic group in table.GroupBy("FieldA", "FieldB")){
    <!-- The dynamic keyword makes it possible to access the values of the grouped fields
        without using square brackets ( ["FieldName"] ) -->
 
     <!-- Return the typed value of one of the grouped fields -->
    @group["FieldA"] 
    @group.FieldB
    
    @foreach(dynamic row in group) {
        <!-- Lets you do the same as you normally do with rows -->
    }
    
    @group.Count <!-- eturns the number of rows in the group -->
    @group.Sum("FieldName") <!-- Return the sum of all the values in the specified column -->
    @group.Min("FieldName") <!-- Return the smallest of all the values in the specified column -->
    @group.Max("FieldName") <!-- Return the largest of all the values in the specified column -->
    @group.Average("FieldName") <!-- Return the average of all the values in the specified column -->
    
    <!-- Further group the data by another field by using .GroupBy(ParamArray pFieldName() As String) -->
    @foreach(dynamic subGroup in group.GroupBy("FieldC")){
        <!-- subGroup is used in the exact same manner as group -->
    }
}

razor web · Perma link post comment Posted by: Sigmund Lunde (11-nov-2013)