Archive for the ‘C# 3.0’ Category

LINQ to DataSets – the Missing Manual

Last week I had to prepare a small presentation for a new LINQ workshop. For my research, I browsed through the MSDN documentation on LINQ where I came across a reference to "LINQ to DataSets". I couldn’t find any more information about it though, and so LINQ to DataSets got a small mention in my [...]

More »

Calculating the Fibonacci Sequence with C# 3.0

Scott Hanselman just posted his latest article in his weekly source code series. He shows various ways of producing the Fibonacci Sequence using various languages. I found it really interesting, for two reasons: I tried to do the C#3.0 one on my own after listening to a podcast about F#, and never could work it [...]

More »

Using LinqPad to Create a Time-Selector Drop-Down List

I am really getting into LINQ now! I think it’s fantastic. I recently wanted to develop a quick drop-down list in ASP.Net which allows a user to select a time of day from a list. The times are 15 minutes apart, so the list would look like this: … 08:00 08:15 08:30 08:45 09:00 … [...]

More »

Using Lambda Expressions with LINQ to SQL

When using LINQ, you need to be careful to use the right kind of Lambda expression. “What, there is more than one kind?”, I hear you gasp. There sure is! And if you aren’t careful, you’ll get a nice little message at runtime to tell you: “System.Object DynamicInvoke(System.Object[])’ has no supported translation to SQL.” What [...]

More »

Inserting into a Table with LINQ to SQL

While creating a little project, I wanted to know how to insert an object into a table using LINQ to SQL. Using Intellisense it wasn’t obvious at all. Scott Guthrie wrote a post on how do it. Unfortunately, the method name has changed since he did it, so you need to call InsertOnSubmit on the [...]

More »

Can You Pass an Anonymous Type Across Functions?

One of the biggest questions with Anonymous Types is “can I pass them around?” If not, why not? Can you do something like this, for example: var GetAnonymousValue() { return new { Name = “Richard Bushnell” }; } void Main() { var value = GetAnonymousValue(); var name = value.Name;} The answer is simple: no, you [...]

More »

Using LINQ To SQL’s DataContext in a Multi-Tier Application

I’m still reading the excellent C# 3.0 in a Nutshell: A Desktop Quick Reference (In a Nutshell (O’Reilly)), by Ben and Joseph Albahari. It answers a popular question: how do I use a DataContext object in LINQ within a multi-tier application? According to the authors, you cannot use the DataContext at an application or static [...]

More »

Fun with C# Extension Methods: Quick Loops

In my last post, I demonstrated an extension method for easy creation of ranges, using the 1.To(x) syntax, similar to the Ruby [1..x] syntax. Today I’m writing another copy of a Ruby idea which lets you do a quick loop using a terse and easy-to-read syntax. Remember, if you want to create your own extension [...]

More »

Fun with C# Extension Methods: Easy Ranges

I’m not a real Ruby on Rails developer, but I’ve tried to learn it, just to broaden my perspective. Coming from a C# background, I’m impressed by how easy it is to read Ruby code. In fact, it is usually so compact and self-descriptive, you can understand it just by reading the code. Imagine not [...]

More »

Refactoring C# Series: Use Automatic Property

Name Use Automatic Property Summary You have a property in a class which just wraps a field of the same type, and simply returns or sets that field. private string _field1; public string Field1 { get { return _field1; } set { _field1 = value; } } Becomes: public string Field1 { get; set; } [...]

More »