Wednesday, December 22, 2021

c# - How to quickly get count of the char in string

The fastest way in c# (counts the commas here):
string s = "Added on Monday, December 20, 2021 2:19:21 PM";
int count = note.Location.Count( x => x == ',' );
Output:
2

c# - How to make generic unique list with GroupBy()

This code make unique book name list + ensures sorting of the list.
private List notes = new List();
...
List uniqueBooks = new List();

uniqueBooks = 
  notes.GroupBy( x => x.BookName ).Select( g => new Note { BookName = g.Key } ).ToList();
uniqueBooks.Sort( (x, y ) => x.BookName.CompareTo( y.BookName ) );

c# - How to convert string into datetime with mask

DateTime dt;                      
bool b = DateTime.TryParseExact( "December 20, 2021", "MMMM d, yyyy", 
                                 new System.Globalization.CultureInfo("en-US"), 
                                 DateTimeStyles.None, out dt );
if ( b ) Console.WriteLine( dt.Year );
Output:
2021
if you have some problem with conversion, try reversal order at first for checking (=>compare if your string is same as output here):
Console.WriteLine( 
DateTime.Now.ToString( "MMMM d, yyyy", new System.Globalization.CultureInfo("en-US") ) );

Thursday, December 16, 2021

D365 - How to prevent warning "BPERRORLABELISTEXT"

A very quick way is to change the quotation marks to apostrophes.
info( "xxx" ) -> info( 'xxx' );
The solution for this warning:

Tuesday, December 14, 2021

D365 - How to solve warning "[BPUnusedStrFmtArgument]:The placeholder '%1' to strFmt is not used in the format string."

Old migrated code from AX 2012:
warning( strFmt( "@cieb:cieb4", _sParamName ) );
shows this warning (in label is this value "Attention - parameter %1 not found."): BP Rule: [BPUnusedStrFmtArgument]:The placeholder '%1' to strFmt is not used in the format string.

 For this warning removing add literalStr() for label string validation.
warning( strFmt( literalStr("@cieb:cieb4"), _sParamName ) );