Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

May 7, 2009

Distinct results sets with ICriteria in NHibernate

by @ 20:56. Filed under NHibernate

If you're using ICriteria to write queries in NHibernate and you're having citeria on items in a one-to-many relation (e.g. retrieve all departments with at least one employee named "John") then you're likely to have experienced that too many objects are returned (at least more than expected). In the example, all departments with multiple "Johns" will be returned once per matching employee. This is probably not what's expected, so here's how to fix it:

// The 'criteria' variable contains criteria on Department

// Create alias to use for employee conditions
ICriteria aliasCriteria = criteria.CreateAlias("Employees", "pr");

// Add criteria on the alias...

// We only want distinct root objects returned
criteria.SetResultTransformer(Transformers.DistinctRootEntity);

The trick is to use Transformers.DistinctRootEntity (at least in NHibernate 2.1). In earlier versions you may want to try something like this:

criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());

Related to this is also how to write criteria that counts the results without actually retrieving them, which can be very useful for paging the result set (i.e. only read a subset of the results at a time). Normally you would write something like this:

criteria.SetProjection(Projections.Count("Id"));
int numberOfItems = (int)criteria.UniqueResult();

However, this suffers from the same problem as described above. To get the correct result, do this:

criteria.SetProjection(Projections.CountDistinct("Id"));
int numberOfItems = (int)criteria.UniqueResult();

Problem solved!

Credits to my colleague Jon for helping out with these rather obscure problem solutions!

/Emil

May 4, 2009

Using PowerShell for login scripts (HTTP POST)

by @ 19:52. Filed under PowerShell

Here's a small example of how to use PowerShell to pretend it's a web browser to login to a service. In my case it's used to let my computer get access to a customer's network, but it could also be used to login to other services, such as public WiFi services, etc. It also serves as an example of how to do a HTTP POST operation in PowerShell, something that can be very useful in many situations.

param($username=($env:username), $password=$(throw "The 'password' parameter is required!"))

$nvc = new-object System.Collections.Specialized.NameValueCollection
$nvc.Add('username',$username)
$nvc.Add('password',$password)
$wc = new-object net.webclient
[void] $wc.UploadValues('http://123.123.123.123/loginuser', $nvc)

The code is very simple:

I think simple examples like this really shows the potential of PowerShell and that making .Net types so easily accessible in the language was a really smart design desicion by the language designers.

/Emil

Calling a PowerShell script from a Windows shortcut

by @ 19:35. Filed under PowerShell

Executing a PowerShell script from outside PowerShell (for example via a Windows shortcut) can be a little complicated, especially if the path to the script contains spaces and it has parameters, so here's how to do it:

powershell.exe -command "& 'C:\A path with spaces\login.ps1' -password XXX"

Note that I'm using the PowerShell & operator to execute the script. This is because I have to surround the path with quotes so that it's interpreted by PowerShell as a single literal even though it contains spaces. If I would leave out the "&" then the quoted string would simply be interpreted as a string literal to output, so the script would not be executed.

The parameters to the script should not be included within the single quotes since they are not part of the first literal, which should only contain the path to the script to execute.

/Emil

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

May 2009
M T W T F S S
« Apr   Aug »
 123
45678910
11121314151617
18192021222324
25262728293031


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

36 queries. 0.636 seconds