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:
// 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:
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:
However, this suffers from the same problem as described above. To get the correct result, do this:
Problem solved!
Credits to my colleague Jon for helping out with these rather obscure problem solutions!
/Emil
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.
$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
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:
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.
36 queries. 0.636 seconds