Mike Taulty's Blog
Bits and Bytes from Microsoft UK

Browse by Tags

Blogs

Mike Taulty's Blog

Elsewhere

  • DataGrid - Master/Details

    Following on from that previous post about the WPF DataGrid I thought it might be interesting to try and move towards more of a master/details view with a separate grid displaying the orders for each customer. I modified the UI a little bit; < Window x:Class ="BlogPost.Window1" xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml" Title ="Window1" Height ="600" Width ="800" xmlns:grid ="http://schemas.microsoft.com/wpf/2008/toolkit" > < Grid > < Grid.Resources > < Style x:Key ="myStyle" TargetType ="{x:Type Control}" > < Setter Property ="Margin" Value ="10" /> < Setter Property ="FontSize" Value ="16" /> </ Style > </ Grid.Resources > < Grid.RowDefinitions > < RowDefinition Height ="Auto" /> < RowDefinition Height ="2*" /> < RowDefinition Height ="2*" /> < RowDefinition Height ="Auto" /> </ Grid.RowDefinitions > < TextBox x:Name ="txtCountry" Style ="{StaticResource...
    Filed under: , , ,
  • Messing with Dynamic LINQ Queries

    Mike was chatting to me about how you'd take something like this ( against LINQ to SQL and the Northwind database ); using (NorthwindDataContext ctx = new NorthwindDataContext() { Log = Console.Out }) { string [] values = { "A" , "B" , "C" }; var query = from c in ctx.Customers select c; foreach ( string s in values) { string t = s; // Care to avoid capturing the same value 3 times... query = query.Where(c => c.CompanyName.Contains(t)); } query.ToList(); } Console.ReadLine(); which produces; SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactT itle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Coun try], [t0].[Phone], [t0].[Fax] FROM [dbo].[Customers] AS [t0] WHERE ([t0].[CompanyName] LIKE @p0) AND ([t0].[CompanyName] LIKE @p1) AND ([t0]. [CompanyName] LIKE @p2) -- @p0: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [%C%] -- @p1: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [%B%] -- @p2: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [%A%] and write a new...
    Filed under: , , ,
  • On LINQ to SQL, Concurrency and Timestamps

    I came across a bit of a glitch in using timestamps for checking concurrency violations in LINQ to SQL and thought I'd share. Say I've got a table like; create table Person ( id int identity primary key , firstName nvarchar(30), lastName nvarchar(30), timestamp ) so, we have a simple table that has a timestamp on it and I want to use that to detect any concurrency problems that I might have in LINQ to SQL. Let's populate this table with some data; insert person(firstname,lastname) values( 'first1' , 'last1' ) insert person(firstname,lastname) values( 'first2' , 'last2' ) insert person(firstname,lastname) values( 'first3' , 'last3' ) and then I can bring that into my LINQ to SQL environment and I can check what the concurrency options are set to; So, you can see that we've got Update Check set to Always and that means that the timestamp will be added to any where clauses for Deletes or Updates and if a particular update doesn't find any row to update because of that where clause then we have a concurrency violation...
  • ACCU Talk on VS 2008

    I'm doing a talk down at ACCU this evening on VS 2008 and, specifically, as it's a short talk focusing on what I think is the biggest single thing in VS 2008 which is what changes around the languages and LINQ. The slides for the talk are here in PPTX , PPT and PDF formats.
  • "When Do Queries Execute"

    Julie's got a great post about when stored procedures execute when using LINQ to Entities . It made me think about LINQ to SQL where if I've got something like; using (NorthwindDataContext ctx = new NorthwindDataContext()) { var query = ctx.GetCustomersByCountry( "UK" ); // SQL Happens Here! foreach (Customer c in query) { Console.WriteLine(c.CustomerID ); } } Then we see the same effect as Julie's seeing in LINQ to Entities (note GetCustomersByCountry is a stored procedure) whereas if I've got something like; using (NorthwindDataContext ctx = new NorthwindDataContext()) { var query = ctx.FnGetCustomersByCountry( "UK" ); foreach (Customer c in query) // SQL Happens Here { Console.WriteLine(c.CustomerID ); } } where FnGetCustomersByCountry is a table-valued function then the query is executed at a different point in time. I guess one reason for this is that table-valued functions are composable in LINQ to SQL so I can do something like; using (NorthwindDataContext ctx = new NorthwindDataContext()) { var query ...
    Filed under: , ,
  • Mocking LINQ to SQL

    Matt Warren has an "interesting" post on how you could hack your way to a mocked implementation of LINQ to SQL - if I had a pound for every time someone's asked me how to do this then I'd have at least 3 pounds :-) Seriously though, there is interest in doing this kind of thing and it's a good starting place if you're inclined to head down this route.
    Filed under: ,
  • Entity Framework Talk at Oracle .NET SIG

    Thanks a lot to the people who came along to my Entity Framework talk at the Oracle and .NET Special Interest Group in London today. As promised, I've uploaded the slides in PPTX and PDF format if you need them for anything.
  • Entity Framework Talk at VBUG Bristol

    Thanks to the people who came along to my talk on Entity Framework at VBUG in Bristol yesterday. I promised that I'd make the slides available and they can be found here for download in PPTX format and PDF format . This was more or less the same talk as I delivered at Southampton the week before so these resources are also relevant.
  • You've got to stop calling it "LINQ" :-)

    <rant strength="veryMild"> A bit of a personal bugbear. I've met a lot of folks who say things like "How do you compare LINQ and traditional data access?" or something along those lines. Please. You've got to stop calling it "LINQ" :-) LINQ is not necessarily anything to do with relational database access. In .NET Framework V3.5 we have; LINQ to Objects LINQ to XML LINQ to SQL ( relational ) "Coming Soon" we have things like; ADO.NET Entity Framework with a LINQ API ( relational ) ADO.NET Data Services where the client code generation pieces take LINQ queries and construct URI's from them. That's at least 5 LINQ pieces from Microsoft already but there are lots of LINQ enabled things going on out there such as; Flickr, Active Directory, SharePoint, etc. etc. etc ( there's a reasonable list up here ) Come on - use "LINQ TO...." when you're talking about LINQ, otherwise everyone (well, me anyway) just gets mixed up :-) </rant>
    Filed under:
  • LINQ to SQL and SqlConnection ( "when you're wrong, you're wrong" :-) )

    I was talking to Ian and another chap at the UK Launch yesterday about LINQ to SQL and my use of the DataContext in some demo code where I always tend to write something like; using (DataContext ctx = new DataContext()) { } and whether I actually needed to do that dispose or not? Now...I was pretty confident that I do need to do this for two reasons; DataContext is disposable so I figure I should probably Dispose() of it - generally been my approach to anything IDisposable. Someone had recently asked me whether DataContext.Dispose() actually did close the SQL connection and I'd done a little bit of reflecting on it and spotted that; DataContext.Dispose() -> SqlProvider.Dispose() -> SqlConnectionManager.DisposeConnection() -> SqlConnection.Close() although the code paths are more complex than that and I haven't attempted to figure out under what conditions the various calls happen. So, I'd kind of reasoned that; If you don't dispose of the DataContext then you won't get the connection closed and back in...
    Filed under: ,
  • LINQ Talk at UK Visual Studio 2008 Launch

    I did a talk yesterday down at the UK Launch of Visual Studio 2008, SQL Server 2008 and Windows Server 2008 about Language Integrated Query as it stands in VS 2008 and .NET Framework V3.5. Firstly, thanks to everyone who came along to the talk - much appreciated. Also a massive thanks to the guys who helped us out with the track at the launch namely; Guy , Andy and Amanda who did a stunning job. An equally massive thanks to the guys who helped us out in the community area, the ATE and the labs who were all doing a great job - I won't list names here but you know who you are and what a big difference you made to the event :-) If you want to catch up on the launch after the event then you can check out the new ( Silverlight based ) Virtual Launch Experience which is up here; Virtual Launch Experience Now...the content isn't 100% the same as what we did in Birmingham so I thought I'd share my session with you here in written form in case you wanted some snippet from it. Firstly, here's the slides in PPTX , PPT and...
    Filed under: , ,
  • LINQ to SQL, Stored Procs, Output Params, Readers Reading

    I had a discussion at DevWeek about when SqlDataReader.Read () gets called for resultsets coming back from stored procedures accessed with LINQ to SQL. I've got this stored proc; create procedure dbo.GetCustomersByCountry @country nvarchar( max ) as set nocount on select * from dbo.customers where country = @country and I've brought it into LINQ to SQL using the designer and that means that I can write code such as; using (NorthwindModelDataContext ctx = new NorthwindModelDataContext()) { var query = ctx.GetCustomersByCountry( "UK" ); foreach (Customer c in query) { Console.WriteLine(c.CustomerID); } } Question is - when does SqlDataReader.Read() get called here? Do we read all of the data up front and then just provide access to an in-memory collection or is the data read from the wire as the loop iterates over it? I wanted to check so I went ahead and set some breakpoints in a few places; Inside the constructor of my Customer class. Inside SqlDataReader.Read() and what I see there is an interleaved set of calls...
  • LINQ to XSD is Back :-)

    A new version of the LINQ to XSD preview that works with Visual Studio 2008. Get the details from here . If you're not so sure on what LINQ to XSD is all about it's essentially for the people who see some LINQ to XML code and say; "But how come the LINQ to XML code has all those ugly strings and casts in it? Can't that be worked out if I have a schema?" That's kind of what LINQ to XSD does for you. Now, if you're a VB programmer then it's a slightly different story as you don't get faced directly with all those ugly casts anyway in LINQ to XML and the tooling also has some clever XSD tricks up its sleeve for VB.
    Filed under: , ,
  • ADO.NET Entity Framework - QueryViews, Inline Functions in SSDL

    I have a mail from Tim on the EF team that I've had for a little while. I'd asked a question about mapping and he'd given me an answer saying something like "Oh, you could always do X, Y, Z" and I'd left this mail in my inbox for a while because I've been distracted and also because I didn't really understand how to do X, Y and Z but I didn't really want to say :-) Today, I think I understood a bit more of it and the result is twofold. The first of those folds is that I'm thinking that I'd really like to spend maybe a week in a darkened room doing nothing but Entity Framework but I doubt that I'll get the chance as (strangely) in my job I'm not really encouraged to learn technology but that's another story and not one that I'll bore you with here. The second of those folds is that I really think that the Entity Framework still has a whole lot of documentation and explanation that's going to be needed if people are really going to get it - there's a lot in there and it's not immediately obvious how to use it and...
  • SQLBits - Birmingham, 1st March

    Just a quick plug :-) I'm speaking at the "SQLBits" conference in Birmingham on the 1st March. The agenda is up here . The agenda is built by voting from prospective attendees and I found it interesting in that I submitted a few sessions that looked something like; "Entity Framework Tour" "LINQ to SQL" Tour "LINQ to Entities" Tour (this is similar to the first topic but with a very different emphasis) "Understanding LINQ" and what surprised me is that the last session was the one that got picked despite it being the session that I'd say is least directly related to SQL - i.e. you can quite happily have some LINQ without bothering with a relational database. So...if you can make the date and the location then sign up for SQL Bits and I look forward to seeing you at my session if it appeals to you. As an aside, if there's something that you'd like to see included in a session like "Understanding LINQ in .NET Framework V3.5 and Beyond" then feel free to ping me a mail letting me know what it is that you want to see...
    Filed under: ,