Friday, December 31, 2010

Linq queries vs Lambda queries


foreach (var id in _clientIds)
{
items.AddRange(
clients.Where(x => x.ClientID == id)
);
}

// rest of the clients
items.AddRange(
clients.Where(x => !_clientIds.Contains(x.ClientID))
);

VS


foreach (var id in _clientIds)
{
items.AddRange(from c in clients
where c.ClientID == id
select c);
}

// rest of the clients
items.AddRange(from c in clients
where !_clientIds.Contains(c.ClientID)
select c);

Saturday, December 25, 2010

Start New Thread

From http://www.csharp-examples.net/create-new-thread/


Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
thread.Start();

public void WorkThreadFunction()
{
// do stuff
}

Friday, December 24, 2010

Open Port Windows 7

As an Administrator:
Windows Firewall with Advanced Security -> Inbound Rules -> New Rules -> Port -> Complete Wizard

Wednesday, December 22, 2010

KeyValuePair vs Dictionary

KeyValuePair or Dictionary for a method that should return a collection of key and values?

Apparently KeyValuePair isn't really intended to be used on its own outside of a Dictionary.

If we look at the signature of both types we'll find that


public struct KeyValuePair <TKey, TValue>
{
}


and for Dictionary:


public class Dictionary<TKey, TValue> : ... IEnumerable<KeyValuePair<TKey, TValue>> ...
{
}


so it's not a matter of KeyValuePair vs Dictionary but really IEnumerable<KeyValuePair<TKey, TValue>> vs Dictionary<TKey, TValue> and
since a dictionary is an IEnumerable<KeyValuePair<TKey, TValue>> then the answer is easy: use a dictionary.

Sunday, December 19, 2010

Install .NET Windows Service - installutil

1. Open the visual studio 2008 command prompt (or the 2010 version) as an administrator
2. Change to the directory where the binaries are deployed
3. Execute the following: installutil MyService.exe

> cd C:\Projects\Builds\MyService
> installutil MyService.exe
> exit

2.- Second Approach

1. open a command prompt as administrator
2. change to the .NET Framwork 2.0 directory
3. execute installutil specifying the full service path

> cd C:\Windows\Microsoft.NET\Framework64\v2.0.50727
> installutil C:\Projects\Builds\MyService\MyService.exe
> exit

If everything was ok, the service is installed and available in the service manager (service.msc)



Monday, December 6, 2010

Rename column

Script:


IF EXISTS (select 1 from sys.objects where name = 'Users') begin -- maybe this IF is redundant.
if exists ( select 1 from sys.tables t
join sys.columns c on c.object_id = t.object_id
where t.name = 'Users' and c.name = 'UserName') begin
exec sp_RENAME 'Users.UserName', 'LoginName' , 'COLUMN'
end
end
GO

Sunday, December 5, 2010

New NOT NULL column on existing table

Script:


if not exists ( select 1 from sys.tables t
join sys.columns c on c.object_id = t.object_id
where t.name = 'PushInterfaceRESTLog' and c.name = 'UserType')
BEGIN
ALTER TABLE PushInterfaceRESTLog
ADD UserType varchar(100) NULL
END
GO

if exists ( select 1 from sys.tables t
join sys.columns c on c.object_id = t.object_id
where t.name = 'PushInterfaceRESTLog' and c.name = 'UserType')
BEGIN
UPDATE PushInterfaceRESTLog SET UserType = 'Vendor'

ALTER TABLE PushInterfaceRESTLog
ALTER COLUMN UserType varchar(100) NOT NULL
END
GO

Saturday, December 4, 2010

Extension Methods

Where records is an IEnumerable of CustomRecord

List<int> numbers = records.GetNumbers();

In a public static class:

public static class DataExtension
{
    public static List<int> GetNumbers(this IEnumerable<CustomRecord> records)
    {
        var numbers = new List<int>();

        foreach (var record in records) {
            numbers.Add(record.Number);
        }

        return numbers;
    }
}