Posts

C# Deserialization and constructor initialization

This is an example how to recreate non serialized member of the class on deserialization. class TestSerialization { [Test] public void Test() { var parent = new Parent(2,3); // serialize byte [] data = SerializationHelper.Serialize(parent); // deserialize var parentCopy = SerializationHelper.Deserialize(data) as Parent; Assert.AreEqual(2, parentCopy.Value1); //confirm that constructor is not called on deserialization Assert.AreEqual(6, parentCopy.Value2); } [Serializable] class Parent { public Parent( int value 1, int value 2) { _value1 = value 1; _value2 = value 2 * 2; //Child object is not serialized, so needs to be recreated on deserialization (see below) _child = new Child() { Value1 = _value1, Value2 = _value2 }; } private int _value1; private int _value2; [NonSerialized] private Child _child; public int Value1 { get { return _child.Value1; } } ...

C# select data using join to temp table

This is useful if you want to select a lot of data by key and writing IN statement is not efficient (too much data etc..). You load the keys to temp table and then select data using a join. CREATE TABLE [dbo].[Table_1]( [Id] [ int ] NOT NULL , [Name] [ varchar ](50) NULL ) [TestFixture] public class TestSelectWithTempTableJoin { [Test] public void Test() { const string nbTempCreate = @" CREATE TABLE #Ids( Id INT ) "; const string nbTempDrop = @" DROP TABLE #Ids "; const string query = @" SELECT * FROM Table_1 t JOIN #Ids temp ON t.Id = temp.Id "; var ids = new List< int >( new int []{1,3,4}); var rows = new List<KeyValuePair< int , string >>(); var sqlConnectionStringBuilder = new SqlConnectionStringBuilder(); sqlConnectionStringBuilder.DataSource = " (local) "; sqlConnectionStringBuilder.InitialCatalog = ...

Get email address from active directory username in C#

* Add reference to System.DirectoryServices static string GetMail( string user) { using (var connection = new DirectoryEntry()) { using (var search = new DirectorySearcher(connection) { Filter = " (samaccountname= " + user + " ) ", PropertiesToLoad = {" mail "}, }) { return ( string ) search.FindOne().Properties[" mail "][0]; } } }

C# Enumerable Split before running Parallel code

[Test] public void SplitWithParallel() { var list = new List< int >( new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); list.Split(4).AsParallel().ForAll(x => x.ForEach(y => Console.WriteLine(y) /*or run a query*/ )); } public static class Extensions { public static IEnumerable<IEnumerable<T>> Split<T>( this IEnumerable<T> list, int parts) { int i = 0; var splits = from item in list group item by i++%parts into part select part.AsEnumerable(); return splits; } public static void ForEach<T>( this IEnumerable<T> source, Action<T> action) { foreach (var item in source) { action(item); } } }

Remote Desktop Connection Manager

Remote Desktop Connection Manager “RDCMan manages multiple remote desktop connections. It is useful for managing server labs or large server farms where you need regular access to each machine such as automated checkin systems and data centers. It is similar to the built-in MMC Remote Desktops snap-in, but more flexible.”

C# LINQ Pivot

public void TestLinqPivot() { var before = new []{ new {Ticker=" FB ", Type=" BID ", Value=1}, new {Ticker=" FB ", Type=" ASK ", Value=11}, new {Ticker=" IBM ", Type=" BID ", Value=2}, new {Ticker=" IBM ", Type=" ASK ", Value=22} }; Console.WriteLine(" Ticker,Type,Value "); foreach (var item in before) { Console.WriteLine(" {0},{1},{2} ", item.Ticker, item.Type, item.Value); } var after = before.GroupBy(x => x.Ticker).Select( x => new { Ticker = x.Key, BID = x.Where(y => y.Type.Equals(" BID ")).Sum(y => y.Value), ASK = x.Where(y => y.Type.Equals(" ASK ")).Sum(y => y.Value) }); Console.WriteLine(); Console.WriteLine(" Ticker,BID,ASK "); foreach (var item in after) { Console.WriteLine(" {0},{1},{2} ", ite...

Embedding file as resource in dll

* In Visual Studio file properties set Build Action as Embedded Resource And then you can read a file using this code private static string ReadResourceFile( string defaultNamespace, string fileName) { var resource = string .Format(" {0}.{1} ", defaultNamespace, fileName); using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)) { using (var sr = new StreamReader(stream)) { return sr.ReadToEnd(); } } }