Wednesday, June 9, 2010

PLINQ with the LINQ to Excel Provider

In .Net 4.0 there are new extensions methods to take advantage of PLINQ (Parallel LINQ). These extension methods can be used quite easily in the LINQ to Excel Provider described in the post. All you need to do is add the AsParallel method to the end of the LINQ query.



ExcelProvider provider = ExcelProvider.Create(@"c:\deploy\Book1.xls");
foreach (Person per in (from p in provider.GetSheet<Person>() where p.LastName == "Johnson" select p).AsParallel())
{
per.LastName = "Smith";
}
Person p = new Person();
p.Id = 10.0;
p.FirstName = "Alex";
p.LastName = "Zander";
p.BirthDate = new DateTime(1980, 4, 4);
provider.GetSheet<Person>() InsertOnSubmit(p);
provider.SubmitChanges();
Console.WriteLine("Done");

Saturday, December 5, 2009

Let Python Speak using COM

What I have wanted to try and do is create a .Net assembly, make it COM accessible, and call it from Python through COM with pyWin32. There are plenty examples out on the web getting this to work for a simple math class or to call to Microsoft Excel. What I thought would be more interesting is to create a .Net assembly to expose the SpeechSynthesizer class so you could send string phrases from Python and have them spoken by the computer. I think that is a cooler example than a Microsoft Excel integration.

Prerequisites:
Python library for Windows: pyWin32
Visual Studio 2005 or greater (I used Microsoft Visual C# 2008 Express Edition)

Step 1. Create the .Net interface and class in a .Net Class Library (DLL)



Step 2. In the class project be sure to select COM Interop



Step 3. Register the .Net Assembly as a COM component from its build location


regasm SolidCode.Speech.dll /tlb: SolidCode.Speech.tlb

Step 4. Run this simple program from Python.

>>> import win32com.client
>>> speech = win32com.client.Dispatch("SolidCode.Speech")
>>> speech.speak('Hello world');

Step 5. Enjoy.

Tuesday, September 15, 2009

Age Extension Methods for .Net DateTime Type

There is not really a straightforward method to calculate a person’s age using the .Net DateTime type. Subtracting years is not a correct means. What is required is to round down a person’s age until that person’s birthday has passed for the given year it is being calculated against. This is what these simple set of extensions to the .Net DateTime class intend to provide.

Monday, September 7, 2009

IsNullOrEmpty Extension Method for .Net Collections

One of the methods on the string class in .Net framework that was added in .Net 2.0 was the IsNullOrEmpty method. This was implemented as a shortcut mechanism for what developers had been doing which was something like:
if (str == null || str.length == 0) {
//do something
}
Or probably more often
if(!(str == null || str.length == 0)) {
//do something
}

If you think about it the string class in .Net is just a specialized type of collection. I see myself performing that similar pattern for other types of collections in .Net. Why not create an IsNullOrEmpty method to support the .Net collection classes and array types?

It is a pretty simple extension and I plan on incorporating this function in any new .Net 3.5 work that I do. It is just a handle shortcut to use.