0

I was hoping you guys can give me an idea on this, I have 3 columns in the database, Emp Name, Emp Schedule, and a bit for fix sched(Boolean). I have a grid that will display their names, and their corresponding schedule for the month and a button that will generate their schedule on the grid.. My question is this, how can I make the generate button work? I mean how can I generate a random schedule for each employee? also their is a condition that if the fix sched column is checked, that employee should not be included in the list..Thanks in advance!

also it should follow this kind of schedule, graveyard shift > mid shift > morning shift.. therefore if an employee already had a morning shift last month his/her next shift should be graveyard shift already..sample codes would be much appreciated.

anonymous1110
  • 885
  • 4
  • 14
  • 28

1 Answers1

4

In SQL:

ORDER BY NEWID()

In LINQ:

order by Guid.NewGuid()
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Is this really good. Because of this http://stackoverflow.com/questions/1705008/simple-proof-that-guid-is-not-unique – Arion Apr 19 '12 at 09:41
  • 1
    +1 Cute idea, I'll remember that. – Adam Houldsworth Apr 19 '12 at 09:46
  • @Arion - From what I can see in that post, the risk of collisions is infinitesimal, definitely not an issue here. Or do you have some other concern? – Oded Apr 19 '12 at 09:46
  • @AdamHouldsworth - It also performs surprisingly well. – Oded Apr 19 '12 at 09:47
  • The main issue would be that GUIDs are designed to be unique, not random, although I suspect that they're probably random enough for the OP's needs in this case. – LukeH Apr 19 '12 at 09:49
  • hi guys,sample codes are much appreciated.. I need to make a logic out of this. tnx – anonymous1110 Apr 19 '12 at 09:50
  • @LukeH - Fair point, though the distribution tends to be fairly random in the SQL Server and BCL implementations. – Oded Apr 19 '12 at 09:51
  • @anonymous1110 Append it to the bottom of your SQL or LINQ statement and it should work. – Adam Houldsworth Apr 19 '12 at 09:51
  • @Oded Re: performance, yeah, I'd say it's a cute idea for small data sets. Lots of rows and you should really double check that it doesn't impact performance too much. – Adam Houldsworth Apr 19 '12 at 09:53
  • @Oded Amended the code slightly, `new Guid()` gives an empty guid. – Adam Houldsworth Apr 19 '12 at 09:56
  • @Oded : My concern was about the collisions. I was just asking what your thought about it. And if this table grows to billion of records can you avoid a collisions then. I thought also about Jasons comment "No! NEWID for unique values, unique is not the same as random. CHECKSUM is for balancing hash tables, not for generating random values. There is no guarantee that any of this is random. No, no, no!" http://stackoverflow.com/questions/10016888/order-by-rand-seems-to-be-less-than-random/10016970#10016970 – Arion Apr 19 '12 at 09:59
  • hi guys, i edited the requirements above =) – anonymous1110 Apr 19 '12 at 10:00
  • @Arion - If one selects a billion records, then yes this could be a problem (though you would have bigger UI problems first). Collisions are possible, but they would also be random... What Jason says is fair, but in reality this works well for small data sets. – Oded Apr 19 '12 at 10:14