2

Thank you all for your comment's and answers, I have this project for monthly employee shifts, I wrote some code, but I got some things wrong, like some Employee are repeating in the same month and that's not supposed to happend. I have more than 100 employees in my table called 'EmployeeTB', and I want to put them in 'ShiftTB', which contains shifts for one month, one by one without repeating employees when I use Random.Next()

here's my code that generates my shifts and it's 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 the code that needs correcting:

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();

How do I solve this?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 1
    Does this answer your question? [Randomize a List](https://stackoverflow.com/questions/273313/randomize-a-listt) – Ken Y-N Oct 04 '22 at 05:37

2 Answers2

1

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

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
A Sad Shoe
  • 134
  • 6
  • thank you for your answer but i get error on EmpList.Shuffle(); My issue is : I have more than 100 employees and I want to distribute them on the days of the month so that they do not repeat during this month – Mohammed D. Q Oct 04 '22 at 06:14
  • the extension method should work, and i can see that it's the exact same way you've done it in your own answer, note that you need to put a static extension method into it's own static class if you want to be able to use it that way, also if you've solved the issue, please mark the answer as such – A Sad Shoe Oct 04 '22 at 08:43
0

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();
  • This is horrible code, btw. You're mutating IDs in a database record and setting a lot of denormalized data into the database. – Enigmativity Oct 05 '22 at 05:43
  • @Enigmativity that's work with me so if you don't like it don't leave comment here , thanks – Mohammed D. Q Oct 07 '22 at 11:04
  • No, it's not a person thing. It's about helping future readers know if this is a good solution. I'm not criticising you, just the code. I'd recommend that you try to write more modular code. A Sad Shoe's code was better because the shuffle was encapsulated in `void Shuffle(this IList list) `. – Enigmativity Oct 07 '22 at 11:17