Home > Software design >  using Random in c# with loop between 2 table
using Random in c# with loop between 2 table

Time:10-04

Thank you all for your comment's and answers i have this project is for monthly shifts for Employee, i wrote some codes but i got some things not correct like some Employee are came to repeating in same month and that's wrong so i have more than 100 staff in my table called EmployeeTB and i want put them in ShiftTB that's only for one month one by one without repeating when i use random function

here's my code that generate my date and day and it's come correct :

private void PopulateDate(DateTime FromDate, DateTime ToDate)
{
    var dt1 = FromDate;
    var dt2 = ToDate;
    var dt = FromDate;
    if (dt <= ToDate)
    {
        dt = dt.AddDays(-1);
        while (dt2 >= dt1)
        {
            List<ShiftTB> ResultList = new List<ShiftTB>
            {
                new ShiftTB { NameOfDay = dt1.DayOfWeek.ToString(), DateOfDay = dt=dt.AddDays(1) },
            };
            foreach (var item in ResultList)
            {

                dt1 = dt1.AddDays(1);
                db.ShiftTBs.InsertOnSubmit(item);
            }
            db.SubmitChanges();
        }
    }
}

and this is my code where i need to correct it:

Random rnd = new Random();
PopulateDate(DateTime.Parse(txtFromDate.Text), DateTime.Parse(txtToDate.Text));

var EmpList = db.EmployeeTBs.Where(x => x.EmpType == "1" && x.Empstatus == "ok").ToList();
for (int i = 0; i < EmpList.Count; i  )
{
    int x = rnd.Next(0, EmpList.Count());
    var ListOfResult = db.ShiftTBs.Where(lor => lor.EmpName == null).ToList();
    ListOfResult[x].EmpID = EmpList[i].EmpID;
    ListOfResult[x].EmpName = EmpList[i].EmpName;
    ListOfResult[x].EmpDepartment = EmpList[i].DepartmentName;
    ListOfResult[x].EmpType = EmpList[i].EmpType;
}
db.SubmitChanges();

Can i get the correct code?

CodePudding user response:

If you only need the list randomized you can use an extension method like the one mentioned in Ken-Y-N's comment (snippet below for the easy version)

private static Random rng = new Random();  

public static void Shuffle<T>(this IList<T> list)  
{  
    int n = list.Count;  
    while (n > 1) {  
        n--;  
        int k = rng.Next(n   1);  
        T value = list[k];  
        list[k] = list[n];  
        list[n] = value;  
    }  
}

This performs a pseudo-random shuffle, which should be plenty for your purposes, if you wanna stick to your own method of generating the randomness, then simply make sure you're marking your Random as static, so if won't grab the same seed etc.

With the above you can structure your code like so:

PopulateDate(DateTime.Parse(txtFromDate.Text), DateTime.Parse(txtToDate.Text));

var EmpList = db.EmployeeTBs.Where(x => x.EmpType == "1" && x.Empstatus == "ok").ToList();
EmpList.Shuffle();
var ListOfResult = db.ShiftTBs.Where(lor => lor.EmpName == null).ToList();
for (int i = 0; i < ListOfResult.Count; i  )
{
    var empId = i % EmpList.Count;
    ListOfResult[i].EmpID = EmpList[empId].EmpID;
    ListOfResult[i].EmpName = EmpList[empId].EmpName;
    ListOfResult[i].EmpDepartment = EmpList[empId].DepartmentName;
    ListOfResult[i].EmpType = EmpList[empId].EmpType;
}
db.SubmitChanges();

This is assuming that you want the list of employees shuffled, and then spacing them out as evenly as possible, if you want it completely random after the list is shuffled you can also do a Random.Next() call as the employee id, but it's up to you. As always, I don't know your exact issue and goal, so take it with a grain of salt

CodePudding user response:

I got the answer here's it :

Random rnd=new Random();
    var EmpList = db.EmployeeTBs.Where(x => x.EmpType == "1" && x.Empstatus == "ok").ToList();
int n = EmpList.Count;
While (n>1){
   n--;
int k = rnd.Next(n 1);
var value = EmpList[k];
EmpList[k] = EmpList[n];
EmpList[n] = value;
}
    var ListOfResult = db.ShiftTBs.Where(lor => lor.EmpName == null).ToList();
    for (int i = 0; i < ListOfResult.Count; i  )
    {
        var empId = i % EmpList.Count;
        ListOfResult[i].EmpID = EmpList[empId].EmpID;
        ListOfResult[i].EmpName = EmpList[empId].EmpName;
        ListOfResult[i].EmpDepartment = EmpList[empId].DepartmentName;
        ListOfResult[i].EmpType = EmpList[empId].EmpType;
    }
    db.SubmitChanges();
  • Related