Thursday, May 19, 2011

Compare data in List<> (Compare two Lists)

List<>.SequenceEqual() can be used to compare data in two sequences.

if you are going to store instances of your own classes, you will need to implement IEquatable and IComparable interfaces in the classes.

        class MyClass : IEquatable, IComparable
        {
            public string text { get; set; }
            public int number { get; set; }

            #region IEquatable<> methods
            public override bool Equals(object other)
            {
                return base.Equals(other as MyClass);
            }

            public bool Equals(MyClass other)
            {
                return (text == other.text) && (number == other.number);
            }

            public override int GetHashCode()
            {
                return text.GetHashCode() ^ number.GetHashCode();
            }
            #endregion

            public int CompareTo(MyClass other)
            {
                int a = this.GetHashCode();
                int b = other.GetHashCode();
                return a.CompareTo(b);
            }
        }

Now you can use SequenceEqual():

        list1.Sort();
        list2.Sort();

        list1.SequenceEqual(list2)


example:


            List list1 = new List()
                {
                    new MyClass(){ text = "text22", number = 2},
                    new MyClass(){ text = "text11", number = 1}
                };

            List list2 = new List()
                {
                    new MyClass(){ text = "text11", number = 1},
                    new MyClass(){ text = "text22", number = 2}
                };

            list1.Sort();
            list2.Sort();

            if (list1.SequenceEqual(list2))
            {
                Console.WriteLine("equal");
            }

Wednesday, May 4, 2011

Read-only tables in MS SQL

here is description how to make readonly table[s] in MS SQL using filegroups:
#1 Create new filegroup;
#2 Add file into the filegroup;
#3 Create new of move existing table[s] into the filegroup;
#4 Change DB mode to SINGLE_USER;
#5 Modify filegroup to be readonly;
#6 Change DB mode to MULTI_USER.

notes:
Table can't be moved into other filegroup using "Alter table".
but you can use "CREATE CLUSTERED INDEX" instead, table will be automatically moved into the new filegroup.

examples:
ALTER DATABASE db_name ADD FILEGROUP FG_READONLY;

    ALTER DATABASE db_name ADD FILE( 
        NAME = 'logic_name_here', 
        FILENAME = 'os_file_name_here', 
        SIZE = 10, 
        FILEGROWTH = 10%,
        MAXSIZE = UNLIMITED ) TO FILEGROUP FG_READONLY;
    GO

    CREATE CLUSTERED INDEX IX_Table_Temp ON dbo.Table(ID) ON FG_READONLY
    GO 

    DROP INDEX IX_Table_Temp ON dbo.Table
    GO

    ALTER DATABASE db_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
    GO
    
    ALTER DATABASE db_name MODIFY FILEGROUP FG_READONLY READONLY;
    GO
    
    ALTER DATABASE db_name SET MULTI_USER;
    GO

links:
Filegroups in SQL Server 2005
How to make a table Read Only in SQL Server