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


Fun with WebDAV

I’m currently in the process of moving this blog from my old server (real piece of iron that was running me US$ 150 / month just to have hosted) to a new VPS from MyHosting (US$ 500 / year).

In trying to publish my new corporate website to (an ASP.NET MVC app) from Visual Studio 2010 to my server using WebDAV.

Sidenote: Microsoft has been trying to get rid of Front Page Server Extensions for a while…  They want us to move to WebDAV for all the reasons we used FPSE, and my new server is running Server 2008 R2 with IIS 7.5 which won’t allow you to install FPSE anymore.  With this cycle of product release MS has decided to force you to use WebDAV.  Configuration of WebDAV is a nightmare, either they need to allow installation / activation / configuration of WebDAV to be as easy as the former FPSE install, or they are going to have to back down (again) and allow FPSE to be run in IIS 7.5. 

So after 2 days of trying to figure out / activate / configure WebDAV it seemed that I finally got it working, so I go to publish my ASP.NET MVC 2.0 web site up to my new server using the Publish Feature in VS2010 for a solution and everything works, UNTIL we hit the “Views” folder, suddenly we get an error

405 - HTTP verb used to access this page is not allowed.

I fire up Expression 4, attach to the website using WebDAV and I can view and add and delete from any folder in the project (controllers, contents, etc) except the “View” folder.  With an MVC project that is pretty much a non starter.  So how did I fix it?  The usual way when MS doesn’t provide good management tools or error diagnostics, I use a sledge hammer (yeah I know my server isn’t a Black Box, but I don’t have time or the will to start trying the 101 different diagnostic pathways to determine what is going on).

I don’t recommend this fix, and someday when I have more time I will go in and figure out the exact necessary and sufficient minimal fix, but for now the answer was:

  1. Open IIS
  2. Select the root website that you want to publish to
  3. Click on the Handler Mappings
    image
  4. scroll down and find the WebDAV handler and click on it then in the Actions click on Editimage
    image
  5. Click on the Request Restrictions
    image
  6. Then click on the Verbs tab, and you will see something like this…
    image
    In my case the verbs in the list were: PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK
  7. Make a copy of those verbs and store them somewhere (see step 11), cause you will want to come back later and unsledge hammer this fix.
  8. Finally just click the “all verbs” radio option and click OK
    image
  9. OK and Apply your way out, and you are good to go!
  10. publish away
  11. Now go back in and PUT the verbs BACK IN, otherwise WebDAV will decided it gets to handle all request before anyone else which must mucks with your site.

As I said, this is definitely a sledge hammer solution and I only recommend it as a work around.  I’ll try to do the 101 diagnostic tests in the next couple of weeks to determine what extra verb I need to add to allow the “View” folder to be accessed / modified.

What so weird is that it only happens on the “Views” folder, I would have expected the same verbs to be used with any of the folders that are published.  What makes the Views folder so unique that it has to use special verbs that the other folders don’t?

Posted on 26 Aug 10 12:13 by matthew.hintzen |

Bookmark this post with:

E-mail | Comments(0) | Comment RSS