Thursday, August 22, 2019

C# - How to use IEnumerable interface with LINQ

/* -- declare class */

public class Country {
  public string Name;
  public string ContinentName;
  public int Population;

  public Country( string _name, string _continentName, int _population ) {
    this.Name = _name;
    this.ContinentName = _continentName;
    this.Population = _population;
  }
}

/* -- init */

List<country> listCountry = new List();

listCountry.Add( new Country( "czech", "europe", 10 ) );
listCountry.Add( new Country( "germany", "europe", 60 ) );
listCountry.Add( new Country( "france", "europe", 60 ) );
listCountry.Add( new Country( "usa", "america", 350 ) );
listCountry.Add( new Country( "china", "asia", 1200 ) );

/* -- IEnumerable -> list of europe items */

IEnumerable<country> europeList = 
  from row in listCountry where row.ContinentName == "europe" select row;

foreach ( Country row in europeList ) Console.WriteLine( row.Name );

Console.WriteLine();
Output:
czech
germany
france

Wednesday, August 21, 2019

C# - How to get sum for field in LINQ, where condition, with both syntax variants

/* -- declare class */

public class Country {
  public string Name;
  public string ContinentName;
  public int Population;

  public Country( string _name, string _continentName, int _population ) {
    this.Name = _name;
    this.ContinentName = _continentName;
    this.Population = _population;
  }
}

/* -- init */

List<country> listCountry = new List();

listCountry.Add( new Country( "czech", "europe", 10 ) );
listCountry.Add( new Country( "germany", "europe", 60 ) );
listCountry.Add( new Country( "france", "europe", 60 ) );
listCountry.Add( new Country( "usa", "america", 350 ) );
listCountry.Add( new Country( "china", "asia", 1200 ) );

double value;

/* -- sum - query syntax variant */

value = ( from row in listCountry
          where row.ContinentName == "europe" 
          select row.Population ).Sum();

Console.WriteLine( string.Format( "Sum of population, europe (variant 1) = {0}", value ) );

/* -- sum - fluent syntax variant */

value = listCountry.Where( w => w.ContinentName == "europe" ).Sum( x => x.Population );

Console.WriteLine( string.Format( "Sum of population, europe (variant 2) = {0}", value ) );
Output:
Sum of population, europe (variant 1) = 130
Sum of population, europe (variant 2) = 130

C# - How to get sum for field in LINQ with both syntax variants

/* -- declare class */

public class Country {
  public string Name;
  public string ContinentName;
  public int Population;

  public Country( string _name, string _continentName, int _population ) {
    this.Name = _name;
    this.ContinentName = _continentName;
    this.Population = _population;
  }
}

/* -- init */

List<country> listCountry = new List();

listCountry.Add( new Country( "czech", "europe", 10 ) );
listCountry.Add( new Country( "germany", "europe", 60 ) );
listCountry.Add( new Country( "france", "europe", 60 ) );
listCountry.Add( new Country( "usa", "america", 350 ) );
listCountry.Add( new Country( "china", "asia", 1200 ) );

double value;

/* -- sum - query syntax variant */

value = ( from row in listCountry                      
          select row.Population ).Sum();

Console.WriteLine( string.Format( "Sum of population(variant 1) = {0}", value ) );

/* -- sum - fluent syntax variant */

value = listCountry.Sum( x => x.Population );

Console.WriteLine( string.Format( "Sum of population (variant 2) = {0}", value ) );
Output:
Sum of population(variant 1) = 1680
Sum of population (variant 2) = 1680

Monday, August 5, 2019

AX - How to work with time zone

This example shows actual hour:

First row is actual time; second row is shifted to user predefined time zone; third row is shifted to specific fixed time zone.
info( int2str( DateTimeUtil::hour( DateTimeUtil::getSystemDateTime() ) ) );
    
info( int2str( DateTimeUtil::hour( 
  DateTimeUtil::applyTimeZoneOffset( DateTimeUtil::getSystemDateTime(), 
  DateTimeUtil::getUserPreferredTimeZone() ) ) ) );

info( int2str( DateTimeUtil::hour( 
  DateTimeUtil::applyTimeZoneOffset( DateTimeUtil::getSystemDateTime(), 
  Timezone::GMTPLUS0100_AMSTERDAM_BERLIN_BERN_ROME ) ) ) );
Output:
7
9
9