Archive for 05:50 PM

Dynamic search with Linq

05:50 PM

The Problem: Find all sentences with either “fox” and “dog” and “fox” or “cat”.


string[] sentences = new string[] {
  "The quick brown fox jumps over the lazy dog",
  "The quick brown fox jumps over the lazy cat"};
string[] findAnd = new string[] { "fox", "dog" };
string[] findOr = new string[] { "dog", "cat" };

The first part (find all) is simple:


var queryand = sentences.AsQueryable();
foreach (string find in findAnd)
{
  string f = find;
  queryand = queryand.Where(s => s.Contains(find));
}

Console.WriteLine(string.Join("\n", queryand.ToArray()));

But how to implement to Or part? Well, google is my best friend. I found the following solution:

Predicate Builder on C# 3.5 in a Nutshell

But this solution does not work with Entity Framework: Predicate Builder and Entity Framework on stackoverflow.com

After implementing this solution your code will look like this:


var predicate = Extensions.False<string>();
foreach (string find in findOr)
{
  string f = find;
  predicate = predicate.Or(s => s.Contains(f));
}
var queryor = sentences.AsQueryable().Where(predicate);
Console.WriteLine(string.Join("\n", queryor.ToArray()));

P.S.: You can find an ExpressionVisitor here: ExpressionVisitor at MSDN

Visual Studio 2008 SDK re-introduces BaseCodeGeneratorWithSite for “Custom Tool” implementation

11:52 AM

A nice detail I thought should be mentioned, since nobody else talks about it:

Many people talk about using BaseCodeGeneratorWithSite with older versions of the Studio and point to a vanished MS Sample which implements the class outside of the Visual Studio SDK since it became internal with the 2003 release. Rejoice! The Visual Studio 2008 SDK 1.1 has the BaseCodeGeneratorWithSite back in its full glory.

(If someone has the Visual Studio 2005 SDK installed I’d be interested to hear about the BaseCodeGeneratorWithSite status there.)

How to use Debian GNU/Linux ‘lenny’ as VirtualBox guest

12:02 PM

My dad has installed lenny (current Debian testing as of 2008-11-something) as guest in VirtualBox. To get the guest utils running, he asked me for help, so I looked into it and this is what I needed to do to get pointer and keyboard integration as well as shared clipboard to work.

First, VirtualBox needs the kernel modules and utilities installed in the guest:

# apt-get install virtualbox-ose-guest-utils module-assistant
# module-assistant auto-install virtualbox-ose-guest

Also, the configuration of the X-Server needs to be adapted. In /etc/X11/xorg.conf, replace the “Configured MouseInputDevice with this section:

Section "InputDevice"
        Identifier "Configured Mouse"
        Driver "vboxmouse"
        Option "CorePointer" "true"
EndSection