Posts

Run 32bit .Net application with >2GB RAM on 64bit machine

To allow your 32bit application use more than 2GB of RAM you need to modify the *.exe file with editbin utility. (open ‘Visual Studio Command Prompt’ and it will be on the path) editbin /LARGEADDRESSAWARE <your-app.exe> or just add these 2 lines to your Post-build event in Visual Studio call "$(DevEnvDir)..\tools\vsvars32.bat" editbin /LARGEADDRESSAWARE "$(TargetPath)" or these if you are building your project with msbuild outside of Visual Studio call "%VS100COMNTOOLS%\vsvars32.bat" editbin /LARGEADDRESSAWARE "$(TargetPath)" To check if all ok run dumpbin utility as below and check if the output has ‘Application can handle large (>2GB) addresses’ text in FILE HEADER VALUES. dumpbin /headers <your-app.exe>

C# Extension to split IEnumerable into batches of n items

This code splits a list into a batches on n items. If list = (1,2,3,4,5,6,7,8,9,10) and n = 3 then the result is ((1,2,3),(4,5,6),(7,8,9),(10)) public static class Extensions { public static IEnumerable<IEnumerable<T>> Batch<T>( this IEnumerable<T> list, int batchSize) { int i = 0; return list.GroupBy(x => (i++ / batchSize)).ToList(); } } [TestFixture] public class TestExtensions { [Test] public void TestBatch() { var list = new List< int >() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var result = list.Batch(3).ToList(); Assert.AreEqual(4, result.Count()); Assert.AreEqual(3, result[0].Count()); Assert.AreEqual(3, result[1].Count()); Assert.AreEqual(3, result[2].Count()); Assert.AreEqual(1, result[3].Count()); result.ForEach(x=>Console.WriteLine( string .Join(" , ",x))); } }

Convert DataTable to IEnumerable for LINQ processing

[Test] public void TestDataTableToEnumeration() { var dt = new DataTable(); dt.Columns.Add(" Id ", typeof ( int )); dt.Columns.Add(" Name ", typeof ( string )); dt.Columns.Add(" Value ", typeof ( double )); for ( int i = 0; i < 5; i++) { var row = dt.NewRow(); row[" Id "] = i; row[" Name "] = " Name " + i; row[" Value "] = i + 0.1d; dt.Rows.Add(row); } var data = dt.AsEnumerable().Select(row => new { Id = ( int ) row[" Id "], Name = ( string ) row[" Name "], Value = ( double ) row[" Value "] }).Where(x => x.Id < 3); foreach (var row in data) { Console.WriteLine(" {0},{1},{2} ", row.Id, row.Name, row.Value); } }

C# Generic Lambda Comparer

public class LambdaComparer<T> : IComparer<T> { public enum Direction { Asc = 1, Desc = -1 } private readonly Comparison<T> _comparison; public LambdaComparer(Comparison<T> comparison) { _comparison = comparison; } int IComparer<T>.Compare(T x, T y) { return _comparison(x, y); } public static LambdaComparer<T> IgnoreSortComparer() { return IgnoreSortComparer(Direction.Asc); } public static LambdaComparer<T> IgnoreSortComparer(Direction direction) { return new LambdaComparer<T>((x, y) => x.Equals(y) ? 0 : ( int )direction); } } public class Data { public int X; public int Y; } [Test] public void TestLambdaComparer() { var data = new Data[] { new Data() { X = 1, Y = 1 }, new Data() { X = 3, Y = 2 }, new Data() { X = 5, Y = 3 }, new Data() { X = 2, Y = 4 }, new Data() { X = 4, Y = 5 } }; Console.WriteLine(...

C# string parameter replacement with simple date arithmetic

[Test] public void FindAndReplace() { var sql = @" SELECT * FROM Table_{@AsOf} WHERE Date='{@AsOf-1}' "; Console.WriteLine(" BEFORE: {0} ", sql); var asOf = DateTime.Today; //(?: )+ means repeat the inner groups var regexExpr = @" (?:{@(?<parameter>\w+)(?<operation>\S)?(?<number>\d+)?})+ "; var regex = new Regex(regexExpr); foreach (Match match in regex.Matches(sql)) { sql = sql.Replace(match.Value, Substitute(match.Value, asOf)); } Console.WriteLine(" AFTER : {0} ", sql); } private string Substitute( string parameterText, DateTime asOf) { var regexExpr = @" {@(?<parameter>\w+)(?<operation>\S)?(?<number>\d+)?} "; var regex = new Regex(regexExpr); var match = regex.Match(parameterText); if (match.Success) { var parameter = match.Groups[" parameter "].Value; if (match.Groups[" operation "].Success &...

Script to create SqlServer Table, Stored Procedure and View

PRINT ' CREATE TABLE ' GO IF EXISTS ( SELECT * FROM dbo.sysobjects where id = object_id(N' [Table1] ') and OBJECTPROPERTY(id, N' IsUserTable ') = 1) DROP TABLE [ Table 1] GO CREATE TABLE [dbo].[ Table 1]( [Id] [ uniqueidentifier ] NOT NULL , [Name] [ varchar ](50) NULL , [DateUpdated] [ datetime ] NULL , CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED ( [Id] ) ) GO GRANT SELECT , INSERT , UPDATE , DELETE ON [ Table 1] TO [SampleUser] GO PRINT ' CREATE STORED PROCEDURE ' GO IF EXISTS ( SELECT * FROM dbo.sysobjects where id = object_id(N' [StoredProcedure1] ') and OBJECTPROPERTY(id, N' IsProcedure ') = 1) DROP PROCEDURE [dbo].[StoredProcedure1] GO CREATE PROCEDURE [dbo].[StoredProcedure1] AS SET NOCOUNT ON UPDATE dbo. Table 1 SET [DateUpdated]=GETDATE() GO GRANT EXECUTE ON [StoredProcedure1] TO [SampleUser] GO PRINT ' CREATE VIEW ' GO ...

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; } } ...