Long Cloud Technologies
"... A Yankee in the Land of the Long White Cloud, Aotearoa ..."

Linq odd behavior and the need for a .ToList() [where I suspect there shouldn’t be any need]

Give an Entity Frameworks model that has an EmployerUsers table, in which there are two columns, Guid and Name  Consider this code…

List<SelectListItem> employerUserSelectList = new List<SelectListItem>( );

var employerUsers = dal.EmployerUsers.Where( eu => eu.EmployerGuid == SecurityHelpers.EmployerGuid );

employerUserSelectList.Add( new SelectListItem( ) { Text = " [ Select TeamMember ] " , Value = Guid.Empty.ToString( ) } );

employerUserSelectList.AddRange(
   employerUsers.ToList( ).Select(
      eu => new SelectListItem( )
      {
         Text = eu.Name ,
         Value = eu.Guid.ToString( )
      }
   )
);

The important piece of code I want you to really look at is this:

 

employerUsers.ToList( ).Select(
   eu => new SelectListItem( )
   {
      Text = eu.Name ,
      Value = eu.Guid.ToString( )
   }
)

now compare that with this…

employerUsers.Select(
   eu => new SelectListItem( )
   {
      Text = eu.Name ,
      Value = eu.Guid.ToString( )
   }
)

Would you or would you not expect these to both work exactly the same?  Well I would but if you try to run the code with the second clause you get the following error

image

System.NotSupportedException was unhandled by user code
  Message=LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
  Source=System.Data.Entity
  StackTrace:
		...SNIP...
  InnerException: 

Yeah, pretty weird, you have to add the .ToList() to coalesce the IQueryable into an IEnumerable BEFORE you call the .Select.

I think this is a bug, either the .Select shouldn’t be allowed on the IQueryable if it can’t handle it, or it should be smart enough to do the .ToList() if necessary.

Your Thoughts?

You can reach me on Twitter at @matthewhintzen:  http://twitter.com/matthewhintzen

Posted on 26 Aug 10 22:58 by matthew.hintzen |

Bookmark this post with:

E-mail | Comments(0) | Comment RSS


Lightspeed issues

I’ve been told that Lightspeed is much better than LinqToSql.  The most touted feature is its round trip, modify the model and the database automatically gets updated.

First time I really tried out this LinqToSql Killer Feature… well reports of LinqToSql’s death have been greatly exaggerated.

image

So why this post… well unfortunately the Mindscape support forum for LightSpeed doesn’t allow direct posting of pictures taken of errors to their forum.  So I have to host the picture somewhere, to set a URL.  Therefore this blog entry will become a ongoing saga of Lightspeed shortcoming, errors and bugs, so that I have a place to set pictures to include in the Mindscape Forum. 

This should be just like watching sausage get made.

Next up  Lightspeed making a HASH and MESS of the Asp.net MVC add view dialog.  If you have Lightspeed in your mvc project go to add a View and decide to create a strong-typed view… your Add View dialog’s dropdown box gets MEGA-FILLED with truly useless things…

image

image

image

Posted on 17 Apr 10 08:57 by matthew.hintzen |

Bookmark this post with:

E-mail | Comments(0) | Comment RSS


InstallShield and Integration with TFS source control

So I’m working with install shield (which hasn’t been my favorite product in the past) and have been trying to get the source control integration built in to work with TFS source control.  Needless to say the help files and forums and knowledge base of InstallShield left ALOT to be desired.

After many Bing and Google searches (trying different word syntax), I found the link I was looking for Visual Studio Team System 2008 Team Foundation Server MSSCCI Provider.

This is what you use for old stuff that doesn’t know how to use the team explorer.  Basically it goes thru the SourceSafe interface.  InstallShield is all happy now

Posted on 27 Jan 10 08:06 by matthew.hintzen |

Bookmark this post with:

E-mail | Comments(0) | Comment RSS


Converting ObjectDataSource to LinqToSqlDataSource

So here I am trying to port an application from asp.net 2.0 pre-Linq IIS7 application to a Windows Azure asp.net 3.5 application converting from old ObjectDataSource to LinqToSqlDataSource.

The first thing I discover is why I find myself more and more as I get older not liking the databinding controls.  They are TOO black box.  If you get an error, often the error is happening inside the black box, with no way to break into the process, examine the variables locals watches and determine exactly what is going wrong.  Instead you get a nice generic error like:

Row not found or changed.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Linq.ChangeConflictException: Row not found or changed.

that tells you nothing!  Ok, what exactly does that mean?  Quick Bing Search (Bing Is Not Google)  and I find this log entry WCSF ObjectContainerDataSource with LINQ to SQL Where in he tells me that in his case the LinqToSql Data Source was trying to find a row using the ID of the row AND the timestamp, if he didn’t include it in the DataKeyNames of the GridView, then he got this error.  Ok so obviously my LinqToSql Data Source needs more information in order to find the row to update, at least that seems logical.  Here is my data diagram

image

in my case the only key is the Uid (Unique identifier – my column name convention for a Guid, and I never include the name of the object as part of the column’s name, there is only one Uid per table and if it is in there it MUST be the unique identifier for that table, less typing, a column name DOES have a object name in-front of “Uid” then it by definition is a ForeignKey back, self documenting columns, but enough with my conventions editorial).  Because it is a Guid this should be sufficient, to find the correct row for updating but in this case LinqToSql Data Source thinks it’s not (for the auto generated update).

I could use a stored procedure, but first let’s see if we can figure this one out.  I see that the ClientProfile is related to Booking by a Has A relationship.  So I’ll make an educated guess that LinqToSql, just to make sure it is finding the correct row, is tacking on an additional lookup against the ClientProfile the booking is related to.  So I decide to add the ClientProfileUid to the DataKeyNames, and Viola, that bug is smashed but now we have another one

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Gee isn’t that helpful?!  And no way for me to break into the place where it is happening and see what object is the problem.

image

So the stack tells me that it is trying to do an Original Values Match and something is null.  Wouldn’t it be REALLY helpful if MS would give me the Key name from the dictionary (second line in the stack)?  But of course they don’t so I’m flying blind in a black box.

I suppose a stored procedure is looking better and better!

I had hoped this blog entry would end with the information necessary to tell us how to get past the problem, but unfortunately, without rewriting the app from scratch (it has some significant warts, due to the fact it was originally a learning exercise for a beginning programmer), 3 hours into it, I still can’t figure it out, and the black box of the DataSource just won’t let me see what the problem is (the break on exception just isn’t working the way it should), I gave up and went back to the stored procedure for the update, as it had been used in the previous incarnation.

This entire app is slated for rewrite upon release of .net 4.0 (and it will be done in MVC with NO BLACK BOXES)!

Posted on 25 Jan 10 08:09 by matthew.hintzen |

Bookmark this post with:

E-mail | Comments(0) | Comment RSS