Show / Hide Table of Contents

    ISqlModelMapper.DistinctCount<TModel>(string expression, params object[] parameters) Method

    .NET Standard 2.x

    Gets the number of all distinct values specified by the SQL expression, according to the criteria specified in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

      public int DistinctCount<TModel>(string expression, params object[] parameters);
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    expression System.String

    A string of SQL expression used to clear up the duplicate data.

    parameters System.Object[]

    (Optional) One or more values that you want to use as retrieval arguments in the SQL SELECT statement defined in TModel.

    Returns

    System.Int32

    Returns the number of rows of the database result set, where duplicates have been cleared up.

    Remarks

    This method uses the SQL COUNT(DISTINCT(expression)) function internally.

    This method only returns the data of the first column in the first row of the retrieved data, therefore, please avoid using this method when groups are defined in TModel.

    Examples

    The following code example demonstrates how to calculate the distinct credits for all courses.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class DistinctCountExample
        {
            private SchoolContext _context;
    
            public DistinctCountExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Lists credits for all courses.
                var courses = mapper.Load<Course>().ToList();
                Console.WriteLine("Credits of courses:");
                foreach (var course in courses)
                {
                    Console.WriteLine(course.Title + " " + course.Credits.ToString());
                }
    
                // Counts the distinct credits for all courses.
                int count = mapper.DistinctCount<Course>("Credits");
    
                Console.WriteLine();
                Console.WriteLine("The number of the distinct credits is {0}.",
                    count.ToString());
    
                /* The code produces the following output:
                Credits of courses: 
                Chemistry: 4
                Physics: 4
                Composition: 3
                Poetry: 2
                Literature: 4
                Trigonometry: 4
                Microeconomics: 3
                Macroeconomics: 3
                Quantitative: 2
    
                The number of the distinct credits is 3.
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Course

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon