LINQ: OrderBy on multiple Fields

by Arnold Matusz 2 11 2008

Most people think that by calling OrderBy().OrderBy() will solves the problem of ordering a sequence by multiple fields. Well I'm dissapointed to announce this is not true because the second call overwrites the ordering clause specified in the first one!

First of all lets analyze the architecture of a LINQ ObjectQuery. The important peice is what it actually returns, namely: an IQueriable object! This IQueriable interface implements the IEnumarble interface which we are normally used to use when creating an ObjectQuery. » Continue reading ...

LINQ: Sequence contains no elements

by Arnold Matusz 28 10 2008

The InvalidOperationException: Sequence contains no elements exception is thrown whenever you try to retreive an element from an empty sequence. This generally happens when you call First on an empty sequence. What I think about for this situation is that I'ld need a method which would return null if the sequence is empty and the first object (maybe the only object) in the sequence otherwise.

In this case the call of FirstOrDefault is recomended. Although after a few searches I've found something similar: we could Single or SingleOrDefault instead of First. Calling these methods is almost exactly what First does except ... this will generate another exception if the sequest contains more elements: InvalidOperationException: Sequence contains more than one element. But the convenient factor is that it quietly returns the single element in a sequence, null of the sequence is empty and the above mentioned InvalidOperationException: Sequence contains more than one element. » Continue reading ...

What's hot in ASP.NET 3.5 SP1

by Arnold Matusz 22 10 2008

First of all it's worth mentioning that Microsoft brought enhancements to the ASP.NET AJAX Extensions. Now we can use a History control which allows us to use the back button to navigate back to a state before the last async postback happened.

Then there is Dynamic Data which is a framework that discoveres the data model and determines the UI which is best suited, dynamically, during runtime. This sounds weird but it can be of big help! Ex: When you try to display a DateTime value in a GridView, you can use a <asp:DynamicField ... /> which will automatically print your DateTime value nicely formatted. To achieve this with the Normal <asp:BoundField ... /> you needed to set HtmlEncode="false" and DataFormatString="{0:D}". If your DataField is a simple string, this will render a LiteralControl. Not only do these work in Read-Only mode, this also happens in Edit mode which can big of big help. » Continue reading ...

About Arnold Matusz

Arnold Matusz

My name is Arnold Matusz. I'm a web developer specialized in .NET technologies with a passion for photography and cars.

View Arnold Matusz's profile on LinkedIn

Who's amung us