Public static class API {// <summary> // create an ordered guid. /// </Summary> /// <returns> </returns> Public static guid newsequentialguid () {guid result; uuidcreatesequential (out result); return result ;} /// <summary> /// http://stackoverflow.com/questions/211498/is-there-a-net-equalent-to-sql-servers-newsequentialid /// </Summary> /// <Param name = "guid"> </param> /// <returns> </ returns> [dllimport ("rpcrt4.dll ", setlasterror = true)] Private Static extern I NT uuidcreatesequential (Out guid); [obsolete ("this is not ordered and the test failed. ")] // <Summary> // http://snipplr.com/view.php? Codeview & id = 29304 /// </Summary> /// <returns> </returns> Public static guid Newcomb () {byte [] datebytes = bitconverter. getbytes (datetime. now. ticks); byte [] guidbytes = guid. newguid (). tobytearray (); // copy the last six bytes from the date to the last six bytes of the guid array. copy (datebytes, datebytes. length-7, guidbytes, guidbytes. length-7, 6); return New GUID (guidbytes );}}
Test
[TestFixture] public class APITest { [Test] public void testUuidCreateSequential() { List<Guid> list = new List<Guid>(); for (int i = 0; i < 100; i++) { Guid id = API.NewSequentialGuid(); list.Add(id); Console.WriteLine(id); } for (int i = 0; i < list.Count - 1; i++) { Assert.Less(list[i], list[i + 1]); } } [Test] public void testNewComb() { List<Guid> list = new List<Guid>(); for (int i = 0; i < 100; i++) { Guid id = API.NewComb(); list.Add(id); Console.WriteLine(id); } for (int i = 0; i < list.Count - 1; i++) { Assert.Less(list[i], list[i + 1]); } } }