31 blog posts tagged LINQ

Creating OR expressions in LINQ

  • đź“…
  • đź“ť 618
  • đź•™ 3
  • 📦 .NET

As everybody who has read my blog before knows, I love LINQ and miss it when coding in other languages, so it’s nice when I get a chance to use it again. When I come back to it with fresh eyes, I notice some things aren’t as easy as they should be — and this time is no exception.

People often need to build up LINQ expressions at runtime based on filters or criteria a user has selected. Adding criteria is incredibly easy, as you can chain operations together on the IQueryable interface, e.g.

Optimizing Sum, Count, Min, Max and Average with LINQ

LINQ is a great tool for C# programmers letting you use familiar syntax with a variety of back-end systems without having to learn another language or paradigm for many query operations.

Ensuring that the queries still perform well can be a bit of a chore and one set that fails quite badly are the aggregate operations when you want more than one.

Include for LINQ to SQL (and maybe other providers)

  • đź“…
  • đź“ť 672
  • đź•™ 3
  • 📦 .NET
  • đź’¬ 12

It’s quite common that when you issue a query you’re going to want to join some additional tables.

In LINQ this can be a big issue as associations are properties and it’s easy to end up issuing a query every time you hit one. This is referred to as the SELECT N+1 problem and tools like EF Profiler can help you find them.

LINQ to SQL tips and tricks #3

  • đź“…
  • đź“ť 925
  • đź•™ 5
  • 📦 .NET
  • đź’¬ 3

Another set of useful and lesser-known LINQ to SQL techniques.

LINQ to SQL supports stored procedures for retrieving entities, insert, update and delete operations, as you know. But you can also use them to perform lazy-loading of navigation properties.

SQL Server query plan cache – what is it and why should you care?

SQL Server like all databases goes through a number of steps when it receives a command. Besides parsing and validating the command text and parameters it looks at the database schema, statistics and indexes to come up with a plan to efficiently query or change your data.

You can view the plan SQL Server comes up with for a given query in SQL Management Studio by selecting Include Actual Execution Plan from the Query menu before running your query.

LINQ to SQL cheat sheet

A few short words to say I’ve put together a cheat sheet for LINQ to SQL with one page for C# and another for VB.NET.

It shows the syntax for a number of common query operations, manipulations and attributes and can be a very useful quick reference :)

LINQ to SQL resources

  • đź“…
  • đź“ť 723
  • đź•™ 4
  • 📦 .NET

A quick round-up of some useful LINQ to SQL related resources that are available for developers. I’ve not used everything on this list myself so don’t take this as personal endorsement.

Note: While the T4 templating language is built-in to Visual Studio 2008/2010 it does not come with syntax highlighting or IntelliSense. Check out either:

LINQ to SQL changes in .NET 4.0

People have been asking via Twitter and the LINQ to SQL forums so here’s a list I put together on a number of the changes made for 4.0.

25 Aug 200925 Aug 2009 — Updated with additional changes, some of which are new in beta 2.

LINQ to SQL tips and tricks #2

  • đź“…
  • đź“ť 534
  • đź•™ 3
  • 📦 .NET
  • đź’¬ 4

A few more useful and lesser-known tips for using LINQ to SQL.

There are times when LINQ to SQL refuses to cook up the TSQL you wanted either because it doesn’t support the feature or because it has a different idea of what makes an optimal query.

LINQ to SQL tips and tricks #1

  • đź“…
  • đź“ť 534
  • đź•™ 3
  • 📦 .NET
  • đź’¬ 4

Being on the inside of a product team often leads to uncovering or stumbling upon lesser-known techniques, and here are a few little nuggets I found interesting — I have more if there is interest.

LINQ to SQL lets you specify that a property is delay-loaded, meaning that not retrieved as part of normal query operations against that entity. This is particularly useful for binary and large text fields such as a photo property on an employee object that is rarely used and would cause a large amount of memory to be consumed on the client, not to mention traffic between the SQL and application.

LINQ to SQL templates updated, now on CodePlex

  • đź“…
  • đź“ť 193
  • đź•™ 1
  • 📦 .NET
  • đź’¬ 12

My templates that allow you to customize the LINQ to SQL code-generation process (normally performed by SQLMetal/LINQ to SQL classes designer) have been updated once again.

CodePlex makes it easier for people to be able to see and merge updates in with their own modified versions as well as report issues via the issue tracker etc. There is also an RSS feed that lets you keep track of releases, source updates or whatever else you are interested in.

Changing type, the state pattern and LINQ to SQL

A question I see from time-to-time on LINQ to SQL relates to changing an entity’s class.

C# and VB.NET don’t allow a class to change its type at run-time and LINQ to SQL specifically doesn’t provide a mechanism for changing the underlying discriminator for this reason.

LINQ to SQL next steps

There has been a flurry of posts and comments in the last 24 hours over the future of LINQ to SQL so I thought it would be interesting to provide some information on what the LINQ to SQL team have been up to and what we’re working on for .NET Framework 4.0.

LINQ was a new feature in .NET 3.5 that provides a store-agnostic query language syntax using a provider model.

LINQ to SQL log to debug window, file, memory or multiple writers

The Log property on a LINQ to SQL data context takes a TextWriter and streams out details of the SQL statements and parameters that are being generated and sent to the server.

Normally in examples you will see Console.Out being assigned to it which is fine for small demos and sandboxes but sooner or later you’ll want access to it in Windows or web applications. Here are some examples of how to redirect TextWriter output such as the DataContext log to other destinations.

LINQ to SQL T4 template reloaded

  • đź“…
  • đź“ť 344
  • đź•™ 2
  • 📦 .NET
  • đź’¬ 10

A newer version of this LINQ to SQL template is available.

The topic of modifying the code generation phase of LINQ to SQL comes up quite often and the limited T4 template I published here last month was good at showing the potential but wasn’t a practical replacement for the code generation phase.

Using LINQ to foreach over an enum in C#

  • đź“…
  • đź“ť 188
  • đź•™ 1
  • 📦 .NET
  • đź’¬ 8

I later expanded this out into a full Enum<T> strongly typed helper.

I can’t be the only person in the world who wants to foreach over the values of an enum otherwise Enum.GetValues(Type enumType) wouldn’t exist in the framework. Alas it didn’t get any generics love in .NET 2.0 and unhelpfully returns an array.

Joining the LINQ to SQL team at Microsoft

I’ve been quiet on my blog lately largely because I have been preparing to change job and relocate half-way around the world to Vancouver in the beautiful province of British Columbia (where I spent my 2004 summer holiday).

In February I traveled out to Redmond for three days of interviews (one position grew to two, then three). Having read the Microsoft Jobs Blog I was prepared for long hard days but in reality the process was incredibly enjoyable and exciting.

Language Integrated Query: An introduction presentation online

  • đź“…
  • đź“ť 96
  • đź•™ 1
  • 📦 .NET, Guernsey

This evening’s presentation on Language Integrated Query (LINQ) is now available from my appearances page.

The event went well with 12 developers (it’s a small island) and took a less time to produce because I didn’t write a script for but slides to lead us through areas I wanted to cover.

Language Integrated Query: An introduction talk tomorrow

  • đź“…
  • đź“ť 128
  • đź•™ 1
  • 📦 .NET, Guernsey

I’m just finishing up the slides, notes and writing code samples for my LINQ presentation at the Guernsey Software Developer Forum tomorrow evening.

Hopefully the broader scope of this presentation will mean a few new faces — the previous talks on Subversion and web application security might have been a little specific for a such a small audience (Guernsey is around 70,000 people on an island 9 miles long).

LINQ presentation at Guernsey Developer Forum

  • đź“…
  • đź“ť 69
  • đź•™ 1
  • 📦 .NET
  • đź’¬ 2

I will be giving a talk at the Guernsey Software Developer Forum at the end of the month on Microsoft’s new Language Integrated Query (LINQ) with particular emphasis on the capabilities and object-relational mapping characteristics of LINQ to SQL.

Now confirmed for:

LINQ in 60 seconds

  • đź“…
  • đź“ť 388
  • đź•™ 2
  • 📦 .NET

Microsoft’s Language INtegrated Query (LINQ) aims to provide a way of selecting objects with a common syntax independent of the data source.

By integrating query into the language instead strings parsed by an external provider at runtime we gain IntelliSense prompting for fields, members and table names and full compile-time syntax checking and a unified syntax.

Partial methods in .NET 3.5, overview and evolution

  • đź“…
  • đź“ť 473
  • đź•™ 3
  • 📦 .NET
  • đź’¬ 1

One of the interesting new things in .NET 3.5 is partial methods which are now being used extensively by LINQ to SQL and no-doubt will be Microsoft’s corner-post of extensibility for generated classes. Here’s a quick overview:

When inheriting from generated classed designers often provide virtual methods for you to override and extend at a cost of being forced to inherit from the generated class instead of one of your own choosing. e.g.

CodeSmith template to generate LINQ To SQL Data Context

If you are interested in what LINQ to SQL generates and don’t have Orcas installed or available right now but use CodeSmith try the following template to generate very similar code.

The primary difference is that this writes out the System types rather than the C# aliases (e.g. System.Int32 instead of int) but that could easily be changed but is binary compatible and otherwise almost identical to the source.

Discarding new entity objects in LINQ to SQL beta 1

  • đź“…
  • đź“ť 404
  • đź•™ 2
  • 📦 .NET
  • đź’¬ 2

An unusual bug in .NET Framework 3.5 beta 1 that relates to creating a new entity object in LINQ to SQL has been getting in my way.

When you create a new entity object it is not automatically added to the database unless you either attach it to the table or create an association to another existing entity.

LINQ to SQL NullReferenceException on SubmitChanges

  • đź“…
  • đź“ť 176
  • đź•™ 1
  • 📦 .NET
  • đź’¬ 5

I’ve been busy working on some LINQ to SQL (formerly DLINQ) apps that have been going well bar a NullReferenceException thrown at me from deep in the bowels of LINQ and it’s change tracker. A cursory glance in debug showed none of the properties that represent columns were null…

The null was actually a collection property that represented a one-to-many relationship that was automatically created using the foreign key constraints in the database. Normally this is set to an empty EntitySet however I was failing to call the default constructor from my new useful constructor.

LINQ to SQL details, issues and patterns

  • đź“…
  • đź“ť 872
  • đź•™ 4
  • 📦 .NET
  • đź’¬ 8

LINQ to SQL (formerly called DLINQ) is a simple object-relational mapper (ORM) scheduled for .NET Framework 3.5/Visual Studio 2007 (Orcas).

On projects with new data I’m keen on keeping the tables and classes as similar as possible and so the limited functionality of LINQ to SQL really appeals to me.

LINQ in C# Web Applications

  • đź“…
  • đź“ť 210
  • đź•™ 1
  • 📦 .NET

I’m a big fan of the Web Application type that was previously available as an add-on to Visual Studio 2005 but thankfully got promoted to a standard citizen with Service Pack 1.

So with a little more time on my hands lately I’ve been delving into the wonder that is LINQ — part of the forthcoming Orcas release of technologies.