Handy code snippet

Handy code snippet

- Read from configuration and set default value if it's null or not set
Solution: (single-line solution) 
var emailFrom = Environment.GetEnvironmentVariable("EmailFrom"EnvironmentVariableTarget.Process);
emailFrom = emailFrom?.ToString() ?? "appsTeam@hotmail.edu.au";

- Check if an item exists in the object collection
Solution: 
if (DKList.Any(e => e.DepartmentCode == "IT"))


- Date time with time zone made easy c# 7 and on
Code:
DateTimeOffset dtwithTimeZone = DateTimeOffset.Now;
DateTime dtOnly = DateTime.Now;
 
Console.WriteLine(dtwithTimeZone);
Console.WriteLine(dtOnly);
Output:
12/09/2019 11:32:15 AM +10:00
12/09/2019 11:32:15 AM

- Unique time stamp in C#
Solution:
Console.WriteLine($"{DateTime.UtcNow:yyyyMMdd-hhmmss-fffff}");
Output:
20190917-121432-72901

- Combine TWO arrays in Dictionary (mapping) and run a Linq query on  Dictionary in C#
Solution:
var map = new Dictionary<stringstring>();
string[] source = new string[] { "cat""dog""fish","cow","frog" };
string[] target = new string[] { "Ishan""Satish""Danial","Danial","Monica" };
map = Enumerable.Range(0, source.Length).ToDictionary(i => source[i], i => target[i]);
map.Add("rabbit""Rohan");
map.Add("turtle""Satish");
 
foreach (var item in map.Where(v => v.Value.Contains("Satish")))
    Console.WriteLine(item.Key);
Output:
dog
turtle

- Different ways to combine objects using Linq or instantiating the object directly  in C#
Solution:
var recipients = new List<EmailAddress>
    {
      new EmailAddress("dk.Shaw@yahoo.com"),
      new EmailAddress("dk.shaw@gmail.com"),
      // new EmailAddress("abc@gmail.com")
    };
msg.AddTos(recipients);
 
// emailCC retrieve from configuration as >> "EmailCc": "dks.poc.it@gmail.com, d.shaw10@gmail.com",
 
List<EmailAddress> recipientsInCC = emailCC.Split(','';').Select(item => new EmailAddress() { Email = item }).ToList();
msg.AddCcs(recipientsInCC);
Output:
collection of email ids in TO nd CC group.

- Replace string between two characters
Solution:
Regex theSubjectRegex = new Regex(@"\{([^\}]+)\}");
emailSub = theSubjectRegex.Replace(emailSub, theDate);
msg.SetSubject($"Azure System Intergration Alert: {emailSub}");
Output:
"Azure System Integration Alert: FTM Payrun Date 07-Nov-19"

- Conver time from one-time zone to another (UTC time to local time zone)
Solution:
DateTime timeUtc = DateTime.UtcNow;
System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfotimeZones = TimeZoneInfo.GetSystemTimeZones();
 
//**Display all timezones**//
foreach (TimeZoneInfo timeZoneInfo in timeZones)
    Console.WriteLine($"{timeZoneInfo.DisplayName}  [{timeZoneInfo.Id}]");
 
//UTC to AEST
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
DateTime timeAEST = TimeZoneInfo.ConvertTimeFromUtc(timeUtccstZone);
Console.WriteLine($"UTC: {timeUtc} AEST: {timeAEST}");
 
//UTC to IST
TimeZoneInfo istZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTimeOffset timeIST = TimeZoneInfo.ConvertTimeFromUtc(timeUtcistZone);
Console.WriteLine($"UTC: {timeUtc} IST: {timeIST}");
Output:
UTC: 12/12/2019 10:01:52 AM AEST: 12/12/2019 9:01:52 PM
UTC: 12/12/2019 10:01:52 AM IST: 12/12/2019 3:31:52 PM +11:00

- Check if a String contains any of some strings (Case insensitive):

if (new[] { "temp""service""fax""community""shared" }.
    Any(c => mydb["distinguishedName"][0].ToString().ToLower().Contains(c)))
{
    Debug.WriteLine("found");
}
else
    Debug.WriteLine("missing");

Comments

Popular Posts