Posts

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(); } } }

Parse XML to object model in C# using XSD utility

Image
* Run xsd Visual Studio command line utility to generate object from xsd file (from Visual Studio Command Prompt) * Then add the cs file to your project (same place as your xsd file) < xsd : schema xmlns : xsd = "http://www.w3.org/2001/XMLSchema" targetNamespace = "urn:contacts" xmlns : bks = "urn:contacts" > < xsd : element name = "contacts" type = "bks:Contacts" /> < xsd : complexType name = "Contacts" > < xsd : sequence > < xsd : element name = "contact" type = "bks:Contact" minOccurs = "0" maxOccurs = "unbounded" /> </ xsd : sequence > </ xsd : complexType > < xsd : complexType name = "Contact" > < xsd : sequence > < xsd : element name = "firstName" type = "xsd:string" /> < xsd : element name ...

Parse XML to dynamic object in C#

<? xml version="1.0" encoding="utf-8" ?> < contacts > < contact id = '1' > < firstName > Michael </ firstName > < lastName > Jordan </ lastName > < age > 40 </ age > < dob > 1965 </ dob > < salary > 100.35 </ salary > </ contact > < contact id = '2' > < firstName > Scottie </ firstName > < lastName > Pippen </ lastName > < age > 38 </ age > < dob > 1967 </ dob > < salary > 55.28 </ salary > </ contact > </ contacts > public class XmlToDynamic { public static void Parse(dynamic parent, XElement node) { if (node.HasElements) { if (node.Elements(node.Elements().First().Name.LocalName).Count() > 1) { //list var item = new ExpandoObject(); var list = new List<dynamic...

C# Scripting with Microsoft Roslyn

If you’re not familiar with Microsoft “Roslyn” please read Interactive C# Microsoft Roslyn . [TestFixture] public class TestScript { [Test] public void TestSimple() { var engine = new ScriptEngine(); Console.WriteLine(engine.Execute(" 1+1 ")); Console.WriteLine(engine.Execute< int >(" 1+1 ")); } [Test] public void TestSession() { var engine = new ScriptEngine(); var session = Session.Create(); engine.Execute(" int x = 1, y; ", session); engine.Execute(" if(x==1)y = 10; else y = 20; ",session); Console.WriteLine(engine.Execute(" y ", session)); } [Test] public void TestScriptFile() { var engine = new ScriptEngine(); ScriptContext context = new ScriptContext(); engine.ExecuteFile(" Test.csx ", context); } [Test] public void TestScriptFileHostObject() { var context = new ScriptContext(); var engine = new ScriptEn...