Thursday, February 25, 2010

LINQ + .ToList() + .ForEach() + Anonymous Methods = Serious Mojo

Just stumbled onto this combination.


XElement root = XElement.Parse(
@"<myxml>
<a/>
<b/>
<c/>
</myxml>");

root.Descendants().ToList().ForEach( delegate(XElement element){ element.Add( new XAttribute( "moo", "car" ) ); } );

Console.WriteLine( root );

Accepting the implications of .ToList'ing your LINQ query, this is some powerful stuff.

PS - If you don't know what I'm talking about with the whole "implications of .ToList'ing your LINQ query" you should really buy the LINQ Pocket Reference. And if you want to easily try the example you should download LINQPad. It's free, excellent, and written by the authors of the aforementioned book.

Add to del.icio.usDiggIt!RedditStumble ThisAdd to Google BookmarksAdd to Yahoo MyWebAdd to Technorati FavesSlashdot it

Thursday, February 11, 2010

The exquisite pain of obscure framework bugs

In hopes of preventing someone else the agony and wasted time I just suffered...

It appears that there's a bug in linq-to-sql with .SubmitChanges() not saving anything. If you implement the "extensibility method" for insert on any of your entities the SubmitChanges silently fails. Actually, I don't know that "fail" is the right word. It simply doesn't save the newly created entity.

Again, the source of the problem is that the insert (i.e. partial void Insert) in your DataContext. For every entity you have the data context will have Insert, Update and Delete methods. E.G.: if you have a Foo entity in your model, the DataContext will get partial void InsertFoo(), partial void UpdateFoo() and partial void DeleteFoo() methods that get triggered when each respective event happens. The only problem is that (at least with the Insert version) if you actually implement this partial in a corresponding partial DataContext class it will *prevent the action from actually happening*.

I don't know why and I don't know the work-around. Perhaps when I have a minute I'll circle back around and try out the Update and Delete versions to confirm the behavior there as well, and maybe try to figure out what the heck is going on.

I hope this just saved you several hours of debugging!

Add to del.icio.usDiggIt!RedditStumble ThisAdd to Google BookmarksAdd to Yahoo MyWebAdd to Technorati FavesSlashdot it

Wednesday, February 3, 2010

Getting -vsdoc.js VS Intellisense to work (for real)

No time for chit-chat but here's one that I don't really want to re-solve...

VS 2008 has a patch to (supposedly) allow for Intellisense on included JavaScript libraries (assuming a -vsdoc.js version is supplied - for example, jQuery).

The idea is that you include the -vsdoc.js version of the file along-side the actual version of the .js file, like this:


<script type="text/javascript" src="Scripts/jquery-1.3.2.js"></script>
<script type="text/javascript" src="Scripts/jquery-1.3.2-vsdoc.js"></script>


And you magically start getting Intellisense for your .js in VS.

Unfortunately, no magic there for me.

Then I found someone saying that it wasn't working for them either, but this worked:


<% if (false) { %>
<script src="Scripts/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
<% } %>


...ostensibly because they were "using user controls". Ugh. The approach here is obvious enough and I actually found a bunch more mentions of this issue & technique out there, even though I'm dubious about what this has to do with user controls or where the disconnect with the original, intended functionality of the patch is. Regardless, this didn't work for me either.

I started to think maybe it was because I was doing MVC, or using a master page, or some file path thing, or... Well I started to run out of ideas right about the time I started running out of new Google results, so I just decided to decompose the problem and work it out for myself. I won't re-hash my tedious and meandering thought process here, but I finally got it working by adding the following to the <head> of my master page:


<script src="%3C%%20=Url.Content%28">" type="text/javascript"></script>
<% if (false) { %>
<script type="text/javascript" src="%7E/Scripts/jquery-1.3.2-vsdoc.js"></script>
<% } %>


I know, crazy. VS seems to understand the ~ and correctly resolve the path to the -vsdoc.js file in the editor just fine. Intellisense shows up in the master and all pages that implement it and everyone's happy.

Including me. Although I'd love to know why it's not working the way it's intended. Unfortunately I don't have the time to track it down. If you do, let me know what you find.

Add to del.icio.usDiggIt!RedditStumble ThisAdd to Google BookmarksAdd to Yahoo MyWebAdd to Technorati FavesSlashdot it