Using the dynamic keyword to transfer anonymous types

If you have an anonymous type in c# and you want to transfer it to another method then you normally have to implement an concrete type and use this type.
If you have the following Linq query:

var result = dataTable.AsEnumerable().Join(
                dataTableKontinent.AsEnumerable(), 
                dr => dr["FK_Kontinent"],
                dr => dr["PK_Kontinent"], 
                (drInner, drOuter) => 
                    new { Name = drInner["AnimalName"], Kontinent = drOuter["Kontinent"] }
                );

The query returns an anonymous type with the properties Name and Kontinent. If you need this type in another Method you have to cast it to dynamic.

foreach (var item in result)
{
     PrintResult((dynamic)item);    
}

The PrintResult method takes an dynamic parameter and you can use the anonymous type in this method. (if you know the properties)

public static void PrintResult(dynamic result)
{
     Console.WriteLine(" {0} wohnt in {1} ", result.Name, result.Kontinent);
}

New tsql commands in Sql Server 2012 (Part 1)

Next year there is a new Sql Server release. In my next posts i will write about some of the TSql commands that are new in Sql Server 2012 (Denali).

Today i will start with the command to create sequences. With the create sequence command it is possible to create a sequence of numbers with a startvalue and an increment value.

Selecting the sequence. Every time the next value for “SequenceName” is called the sequence gets incremented by the defined value.

Result of the selection.

The sequence has some additional keywords. The minvalue defines the minimum value of the sequence the maxvalue defines the maximum value of the sequence and the cycle keyword defines that the sequence reuses the values. That means that if the sequence reaches his maxvalue the sequence count starts at the begining. The following sequence uses the minvalue, maxvalue and cycle keyword.

Selecting the sequnce. Every time the next value for CyclingSequence is called the sequence is incremented by 25. When it reaches the maxvalue it starts again from the minvalue.

Result of the selection.


async in the .net framework 4.5

In the developer preview of the .net framework 4.5 the new async feature is included. Microsoft indroduced two new keywords to make asynchronos
programming much easier. This two new Keywords are:

  • async
  • await

It is only possible to use await in a method that is marked with the async keyword. In this simple example the usage of async and await is shown.

 public async void SimpleAsync()
 {
      WebClient webClient = new WebClient();
      await webClient.DownloadStringTaskAsync("http://www.orf.at");
 }

This example shows how the async pattern works. First you have to declare the method as async. If a method is marked as async it is possible to use the await keyword  in it.
If the execution path hits the await keyword it is executing the asynchron operation and imediatly returns to the calling method. If the asynchron operation finished the rest of the method is executed. That means that the compiler is generating a ContinueWith Task for you and puts all statements behind the await keyword in this ContinueWith Task. To do this the compiler generates a state maschine. This statemaschine works similar to the statemaschine the compiler generates if you use the yield statement. The following code comes out of ilspy and shows the code the compiler generates.

public void SimpleAsync()
{
    Program.d__0 d__0 = new Program.d__0(0);
    d__0.4__this = this; 
    d__0.t__MoveNextDelegate = new Action(d__0.MoveNext);
    d__0.$builder = AsyncVoidMethodBuilder.Create();
    d__0.MoveNext();
}

As we see the compiler generates an special class to handle the async execution.

private sealed class d__0
{
    private int 1__state;
    private bool $__disposing;
    public AsyncVoidMethodBuilder $builder;
    public Action t__MoveNextDelegate;
    public Program 4__this;
    private TaskAwaiter<string> t__$await2;
    [DebuggerHidden]
    public d__0(int 1__state)
    {
        this.1__state = 1__state;
    }
    public void MoveNext()
    {
        AsyncVoidMethodBuilder asyncVoidMethodBuilder;
        try
        {
            int num = this.1__state;
            if (num != 1)
            {
                if (this.1__state == -1)
                {
                    return;
                }
                this.t__$await2 = AsyncCtpThreadingExtensions.GetAwaiter<string>(AsyncCtpExtensions.DownloadStringTaskAsync(new WebClient(), new Uri("http://www.weather.gov")));
                if (!this.t__$await2.IsCompleted)
                {
                    this.1__state = 1;
                    this.t__$await2.OnCompleted(this.t__MoveNextDelegate);
                    return;
                }
            }
            else
            {
                this.1__state = 0;
            }
            string result = this.t__$await2.GetResult();
            this.t__$await2 = default(TaskAwaiter<string>);
            this.4__this.WriteLinePageTitle(result);
        }
        catch (Exception arg_AB_0)
        {
            Exception exception = arg_AB_0;
            this.1__state = -1;
            asyncVoidMethodBuilder = this.$builder;
            asyncVoidMethodBuilder.SetException(exception);
            return;
        }
        this.1__state = -1;
        asyncVoidMethodBuilder = this.$builder;
        asyncVoidMethodBuilder.SetResult();
    }
}

So the compiler generates all the importent asynchron code. To execute an asynchron operation you only need to declare an method as async and then you have to tell the compiler on wich operation you want to wait. Befor this operation you put the await keyword and the compiler does the rest.