How to select N Random Items from the List in C#

February 20, 2022 0 comments
This post is about, how to select N Random Items from any T list in C# 

 e.g. Say we have IList with total count of 1000 and we need to get 100 random items from the List. We should use following function to return N Random Items from any List in C#

public IEnumerable <T> PickRandom<T>(IEnumerable<T> list, int number)
{
    var random = new Random();
    var total = list.Count();
    var required = number;
    foreach (var item in list)
    {
        if (random.Next(total) < required)
        {
            required--;
            yield return item;
            if (required == 0)
            {
                break;
            }
        }
        total--;
    }
}

Calculate next due date based on start date frequency and current date in c#

April 17, 2021 0 comments

During one project development there is one scenario which made me write this post.
Scenario is as follows
Company has clients, each has different invoice start date and invoice frequency. I had to calculate and display next invoice date from current date + next month. i.e. invoice will always be raised in next month of service completion. e.g. Client A has invoice start date 01-Jan-2021 and invoice frequency is 5 months. Date when I am writing this post is 17-April-2021. So now next invoice date for Client A is 01-June-2021. For client B invoice start date is 01-March-2021 and invoice frequency is 3 months, then its next invoice date is 01-June-2021 and so on for all other client. 

To handle this scenarios I have written following function in c# which will always returns next invoice date from Current date. 


public class DateHelper
{
 public static DateTime nextInvoiceDate(DateTime? InvoicingStartDate,
    string invoicingFrequency)
     {
     int occurrence =
     (DateTime.Now.Month - Convert.ToInt32(InvoicingStartDate?.Month)) 
     / Convert.ToInt32(invoicingFrequency);
     
     int restInt = 
     (DateTime.Now.Month - Convert.ToInt32(InvoicingStartDate?.Month)) 
     % Convert.ToInt32(invoicingFrequency);
     DateTime next = InvoicingStartDate.Value;
            int multi;
            if (restInt == 0 && DateTime.Now < InvoicingStartDate)
            {
                if (Convert.ToInt32(invoicingFrequency) == 1 && occurrence < 0)
                {
                    multi = 1;
                }
                else
                {
                    multi = Convert.ToInt32(invoicingFrequency)
                    * Math.Abs(occurrence);
                }
                
            }
            else
            {
                if (Convert.ToInt32(invoicingFrequency) == 1)
                {
                    multi = Convert.ToInt32(invoicingFrequency)
                    * (Math.Abs(occurrence));
                }
                else
                {
                    multi = Convert.ToInt32(invoicingFrequency) 
                    * (Math.Abs(occurrence) + 1);
                }

            }
            return next.AddMonths(multi);

        }
    }
  • How to calculate a future date based on a given date ?
  • Calculate next due date based on start date frequency and current date ?
  • How to nearest future date from given date and frequency ?
  • Best way to calculate next date from start plus frequency
  • How to calculate next meeting date based on given frequency ?

The razortaghelper task failed unexpectedly

December 01, 2020 0 comments

 After updating Visual studio to New version, I suddenly started getting error 

the razortaghelper task failed unexpectedly


Well after doing some research on Internet I came across following solutions


Added the following environment variable to my user environment to make it build again:
DOTNET_HOST_PATH

%ProgramFiles%\dotnet\dotnet.exe 


Simply go to Environment Variable, check following images


Environment Variable

 

After clicking on Environment Variables, click on New, Pop up will open up, simply add entries as per following image and click Ok

Updating Environment variables for the razortaghelper task failed unexpectedly

Now when you build The razortaghelper task failed unexpectedly error will go away.