Rick Strahl's Web Log
Wind, waves, code and everything in between...
WebLog Posts in Category LINQ
LINQ to SQL and missing Many to Many EntityRefs
December 24, 2009 @ 2:05am
LINQ to SQL expects every table to be mapped to have primary keys which is reasonable. However, today I ran into a problem with an existing database and a many to many relationship which did not have a Pk in the link table. The model and underlying XML seemed to map this just fine, however, the actual generated code did not approve of this arrangement.
LINQ to SQL, Lazy Loading and Prefetching
October 12, 2009 @ 2:56am
Lazy loading in an ORM can be really useful or a royal pain if you walk through a lot of data. LINQ to SQL uses lazy loading of related entities and entity sets but there are a couple of ways that allow you to do eager loading instead. Both require some extra effort and foresight.
Fun with Func<T,TResult> Delegates, Events and Async Operations
October 05, 2009 @ 1:38am
Delegates are powerful for event handling, highly useful in LINQ and a core requirement for async operations. Func
makes using delegate based logic a lot easier by providing a generic implemenation that in many cases allows you to skip creation of a separate delegate and instead just point at the Func
definition which can be a big timesaver and a nice way to remove delegate defintions from your namespaces.
System.Data.Linq.Binary is not XmlSerializable
July 16, 2009 @ 11:30pm
LINQ to SQL generates TimeStamp fields as System.Data.Linq.Binary types in the data model it creates. This type unfortunately doesn't serialize and XmlSerializer and therefore Web Services in general fail with a downright serialization failure. It's possible to work around this by using internal scope but it isn't a perfect solution.
LINQ to SQL and Transactions
July 14, 2009 @ 12:48am
Using explicit Transactions with Linq to Sql is fairly rare but when you need them you'll find that it natively doesn't provide manual transaction management. Rather you have to rely on the TransactionScope class to provide transaction wrapping. Here's a contrived example, and a couple of different transaction scenarios using TransactionScope to manage multiple L2S data operation as a single transaction.
LINQ to SQL and alternate Providers
May 18, 2009 @ 3:12am
I’ve tried an interesting experiment today trying to see if I can get LINQ to SQL to run with a custom ADO.NET Data Provider I’ve created some time ago. Basically I implemented a provider that is a proxy to a remote Web ‘service’ that can marshal SQL commands via Web Requests on a remote server. I implemented a custom provider for this interface that ships XML across the wire and marshals...
LINQ to SQL DBML fails to build or open
October 10, 2008 @ 3:50am
Arrgh. Today I got back to a LINQ to SQL app that’s been sitting idle for some time. It’s a demo app and as I started the app up and compiled the solution I got: Error 1 Build failed due to validation errors in C:\projects2008\TimeTrakker\TimeTrakkerBusiness\model\TimeTrakker.dbml. Open the file and resolve the issues in the Error List, then try rebuilding the project....
LINQPad as a Code Snippet Execution Engine
April 17, 2008 @ 12:45am
LINQPad is an awesome tool to quickly execute code. Besides the obvious LINQ testing features that its name implies, LINQPad also can come in quite handy as a generic .NET code snippet execution utility that allows you to run any .NET expression or single statement block. It's a great tool for your toolbox to quickly check behavior of BCL functions for example...
Dynamic Queries and LINQ Expressions
April 14, 2008 @ 12:58am
Using LINQ to SQL in some scenarios where dynamic expresions are required on the right side of a LINQ query expression can be tricky. How do you for example allow a query to use a dynamic field in a query and then query its data? Due to the strong typed nature of LINQ direct LINQ syntax can't be used for this sort of query, but there are a several ways to work around this by using dynamic expressions.
LINQ to SQL ObjectTrackingEnabled and Deferred Loading of Related Data
April 12, 2008 @ 3:28pm
LINQ to SQL supports object tracking by default, but if running queries it's often more efficient to turn off object tracking. This can be done with ObjectTrackingEnabled, but unfortunately this option has a rather severe side effect on deferred object loading.
LINQifying - getting rid of CollectionBase?
March 26, 2008 @ 1:57am
If you're sitting on some old types that implement CollectionBase you might find that they don't directly work with LINQ. Here's how you can make these types work with LINQ.
Update to LINQ to SQL Business Object Wrapper Sample
February 05, 2008 @ 2:24pm
Last night I after I posted the LINQ DataContext post I realized that I hadn't ever updated the LINQ Business Object Layer example app for .NET 3.5 RTM. Although I've long updated the code, since I use it here in a few internal applications as a base business object wrapper around LINQ to SQL, I...
Linq to SQL DataContext Lifetime Management
February 05, 2008 @ 1:55am
The DataContext is the central object that holds LINQ to SQL together. This post talks about a few different approaches you can use to hang on to your DataContext in the course of a request, to pass between layers or to manage on a per thread/request level.
LINQ To SQL and the Web.Config ConnectionString Value
December 07, 2007 @ 1:49am
I just got had by an odd behavior in the LINQ to SQL model designer when working with a LINQ to SQL Model in a separate class library project. I use LINQ to SQL in a business layer which always lives in a separate assembly and so the model does not live in the same project as the Web (or other)...
Anonymous Types in C# 3.0
November 15, 2007 @ 4:25pm
One of the most convenient features of C# 3.0 is the ability to create new types 'on the fly' using Anonymous Types. Anonymous Types are essentially compiler generated types that you don't explicitly declare with an official type declaration. Rather you define the type inline as part of the code...
Type Intializers in C# 3.0
November 12, 2007 @ 1:20am
During my LINQ to SQL session at DevConnections somebody in the audience asked what happens behind the scenes when you use type initializers in C# 3.0 and well it turns out I gave a hedged answer with a guess that was - uhm - wrong. So here's a discussion of how this feature actually works....
Complex Detached Entities in LINQ to SQL - more Nightmares
October 01, 2007 @ 12:04am
Ok, so I thought I had the detached entity state in LINQ to SQL figured out, only to have that thrown back into my face. The word from Microsoft is that if you have an entity that was loaded through a LINQ to SQL DataContext at any point it cannot be re-attached to the DataContext.
A simple Business Object wrapper for LINQ to SQL
September 27, 2007 @ 4:59am
I've been talking about how I'm using a small business object wrapper around LINQ to SQL to provide better abstraction of the LINQ to SQL functionality and I suppose the time's come to be bit more specific. This is going to be a long post with a fair bit of code and a brief discussion of the whys and hows...
LINQ to SQL Queries out of a Business Layer
September 26, 2007 @ 12:43am
Using LINQ to SQL for queries out of a business layer has some cool benefits in that core business layer queries returned from business objects can further refine query before the final query operates against the database. This allows the client to perform common formatting like picking which fields to retrieve, changing order or applying a final front end filter on the data. It also allows the client to choose best to represent the query results as an entity list or data reader/data table etc. This entry shows a few ways of how this has advantages and how a business object can greatly facilitate the data serving process.
LINQ to SQL Serialization bites me again
September 21, 2007 @ 12:01am
I'm mucking around with a bit of code today that's using AJAX and returning some data from my business layer that's using LINQ to SQL Entities. I mentioned some time that one issue that I ran into and continue to bump my head against is that LINQ to SQL has big issues serializing its entities. The...
First Impressions on LINQ to SQL
September 11, 2007 @ 12:45pm
I've been experimenting with LINQ to SQL for the last few weeks and there's a lot to like as well as a few things to think about. In this editorial I'll describe some of the things that worked well and some of the issues I struggled with and how I worked around them.
LINQ to SQL and Serialization
September 02, 2007 @ 10:40pm
LINQ to SQL provides entity objects that can be used easily for object access to data members. But these objects can be difficult to deal with if you need to serialize them in Web Services or in other ways because relationships are often circular. Here's a discussion of things to watch out for and how you can get around the issues.
Version Fields and LINQ: Careful of underscores in Field Name
August 28, 2007 @ 4:46pm
This probably falls into the bonehead category, but this might bite somebody else so I'm writing it up: LINQ to SQL works best when your database tables have version fields that can be used to see change state. So recluctantly I resigned myself to give in to this and decided I'd use a consistent...
Dynamic Expressions in Linq to Sql
August 28, 2007 @ 12:43am
Using LINQ there are two issues that I've been struggling with and both have to do with dynamic query results: Hitting the wall with SQL that can't be expressed via LINQ and using dynamic expressions inside of LINQ queries from within framework level code. As it turns out there are ways that this can be addressed with lower level LINQ methods that provide the abillty to be a bit more flexible in controlling what SQL gets executed and creating dynamic expressions for results.
LINQ To Sql - Getting a Command Object from a Query
August 24, 2007 @ 3:24am
LINQ to SQL is all about returning object from database queries, but sometimes it's actually useful to use good old DataReaders or DataTables - especially in ASP.NET applications that use DataBinding. Entities are objects and databinding requires Reflection which is very slow, compared to a DataReader. Luckily LINQ to SQL provides the abillity to retrieve a Sql Command object that can be used to return an alternate result set.
Multiple Enumeration of LINQ results
August 16, 2007 @ 12:02am
LINQ result set enumeration can be a little unexpected if you don't pay close attention the returned Enumeration. Because of the way queries are enumerated at enumeration time, not when they are declared can easily result in multiple queries being run against the LINQ data or list source.
LINQ to SQL and Disconnected Entities Follow-up
August 15, 2007 @ 12:17am
Several people have been flogging me for my LINQ and disconnected Entity post, so I thought I should follow up on the issues I've been having. First let me restate Entity issue I raised a couple of days ago, but let me present it in a simpler context with the Northwind database, so you can try...
LINQ to SQL and Dynamic Queries and Expressions?
August 13, 2007 @ 12:02pm
Continuing my experiments with LINQ in the middle tier I ran into another problem: Inability to make LINQ run any dynamic operations that are not defined explicitly in the LINQ to SQL import. For building a generic business framework that doesn't have concrete type references (to say a PK field) but dynamically needs to create SQL to query data. Even relatively simple things like retrieve a single record by primary key where primary key is dynamic not explicit is not easily accomplished without getting real low level into Lambda expression creation.
LINQ to SQL and attaching Entities
August 12, 2007 @ 6:49pm
I have been experimenting with LINQ to SQL in the middle tier and have run into the apparently common issue of re-attaching entities to a DataContext in cross tier scenarios. Either the support re-attachment is not there or it is broken but either way it doesn't work which makes it pretty tough to use LINQ to SQL in the middle tier.
LINQ and dynamic Query Results?
March 22, 2007 @ 1:03am
I've been mostly watching LINQ take shape and only recently started in looking at it a bit more closely.I've been pretty excited about the prospect of LINQ in general and the database connectivity in particular - after all I come from an XBase background where a built in Data Definition Language...
December, 2014 (3)
November, 2014 (2)
October, 2014 (3)
September, 2014 (2)
August, 2014 (1)
July, 2014 (2)
June, 2014 (4)
May, 2014 (3)
April, 2014 (1)
March, 2014 (2)
February, 2014 (2)
January, 2014 (3)
December, 2013 (4)
November, 2013 (5)
October, 2013 (4)
September, 2013 (3)
August, 2013 (2)
July, 2013 (3)
June, 2013 (4)
May, 2013 (2)
April, 2013 (5)
March, 2013 (4)
February, 2013 (2)
December, 2012 (2)
Visual Studio (37)
Web Services (17)
Web Api (16)
Microsoft AJAX (8)
Web Connection (7)
Sql Server (7)
West Wind Ajax Toolkit (7)
Live Writer (5)
Html Help Builder (5)
Entity Framework (5)
Visual Studio (4)
Source Control (4)
Software Development (3)
Help Builder (3)
Internet Explorer (2)
Web Deployment Projects (1)
Web Design (1)
Control Development (1)
Dynamic Types (1)
ASP.NET vNext (1)
Posts - 1011
Comments - 13015
Rick's GitHub Projects
Rick's FoxPro Web Log
Debugging with Fiddler: The complete reference from the creator of the Fiddler Web Debugger
by Eric Lawrence
WPF 4 Unleashed
by Adam Nathan
ASP.Net MVC in Action
by Jeffrey Palermo, Ben Scheirman, Jimmy Bogard
Tweets by @RickStrahl
Find this content useful? Consider making a small donation to show your support.
© Rick Strahl, West Wind Technologies, 2005 - 2014