Codepolice

Random posts from Ola in English. Mainly about programming and the web.

I love Google – Extract a favicon to a png

Sometimes i love Google more then is healthy. I was looking for some way to extract a favicon from a page and get it as a png, and i found this wonderful little service.

http://www.google.com/s2/favicons?domain=youtube.com

http://www.google.com/s2/favicons?domain=yourdomain.com

Anf you get the icon in a png. Amazing!

Join and Group By with LINQ to Entities

I have banged my head against some LINQ to Entities stuff today. It started out when i was doing a query to get some stats out of my forum. I should point out that i’m a newbe with LINQ to Entities and the Entity Framework so if i have done anything stupid here you are free to mock me in the comments.

var query = (from s in db.ForumStatsSet
where s.LogDate >= date1 && s.LogDate <= date2
group s by new {
  s.Topic.topicID, s.Topic.subject,
  s.Topic.datum, s.Topic.Forum.forumID,
  s.Topic.Forum.forumName,
  s.Topic.Forum.ForumGroup.name } into g
orderby g.Count() descending
select new TopicStatsData
  {
  TopicId = g.Key.topicID,
  Count = g.Count(),
  Subject = g.Key.subject,
  ForumId = g.Key.forumID,
  ForumName = g.Key.forumName,
  ForumGroupName = g.Key.name
});

This code looked fine to me. It’s kind of massive and a lot of joins but it should be used in a admin interface so i did not care that much. But it turned out that it timeout every time in ran it. I had a look at the SQL that was generated and that was not a pretty sight. A massive amount of sub queries and left outer joins.

I posted a question on the new and really really good code community Stackoverflow and got the tip that i should join the tables before group by clue. And that is what i tried to do.

The problem was that i got this error all the time.

from s in db.ForumStatsSet
from t in s.Topic

Error:
An expression of type ‘WhoaAdmin.Models.Topic’ is not allowed in a subsequent from clause in a query expression with source type ‘System.Data.Objects.ObjectQuery<WhoaAdmin.Models.ForumStats>’. Type inference failed in the call to ‘SelectMany’.

I tried to google that error and i got nothing. I realised that maybe you have to do the joins in the correct order and that was it and i ended up with this query.

var query = (from fg in db.ForumGroupSet
from f in fg.Forums
from t in f.Topics
from s in t.ForumStats
where s.LogDate >= date1 && s.LogDate <= date2
group s by new { t.topicID,
  t.subject,
  t.datum,
  f.forumID,
  f.forumName,
  fg.name } into g
orderby g.Count() descending
select new TopicStatsData
{
 TopicId = g.Key.topicID,
 Count = g.Count(),
 Subject = g.Key.subject,
 ForumId = g.Key.forumID,
 ForumName = g.Key.forumName,
 ForumGroupName = g.Key.name
});

The SQL i ended up with looks kind of awful but probably the SQL engine do something like this “under the hood” on regular SQL group by to. At least it is kind of fast. The big diffrence in the generated SQL is that it has replaced all “LEFT OUTER JOINS” with “INNER JOINS” and they are of course faster.

Please leave comment.

Group By with LINQ

Today i wanted to do a group by query with LINQ wich is something i never done before. I turned out to be kind of easy.

If you just want to group by a single field.

(from s in db.CrewStatsSet
where s.LogDate >= date1 && s.LogDate <= date2 && s.Action == action
group s by s.User.nick into g
orderby g.Count() descending
select new CrewStatsData
{
    Nick = g.Key,
    Count = g.Count(),
}).ToList();

And if you need to group by multiple fields.

(from s in db.CrewStatsSet
where s.LogDate >= date1 && s.LogDate <= date2 && s.Action == action
group s by new { s.User.nick, s.Action, s.User.userID } into g
orderby g.Count() descending
select new CrewStatsData
{
    Nick = g.Key.nick,
    Count = g.Count(),
    ActionId = g.Key.Action,
    UserId = g.Key.userID
}).ToList();

Finally have Subversion + CruiseControl.NET + MSBuild running

I’ve been struggling the whole afternoon to get some kind of continuous integration up and running. It started with that i got sick and tired of the Publish feature in Visual Studio 2008 because it just starts to fail from time to time and its kind of impossible to find out why because it doesn’t give any kind of error information.

Anyway i started to look at MSBuild and see if it could handle my Publish task and i found this great article. It explains how to make a build file that build your project and then also simulate the publish feature and put just the files necessary into a separate folder.

While i was on the subject i thought that i could as well see if i could set up my source control machine to build my app as soon as i check in some files, copy the build to a folder and make that folder a web application.

I actually was kind of easy. I found an article by Omar Al Zabir that explains this in great detail.

Not any problem at all?

Ofcourse. The one i had most problem with was the infamous Microsoft.WebApplication.targets bug. When you build your project on the build machine you get this error.

error MSB4019: The imported project “C:Program FilesMSBuildMicrosoftVisualStudiov9.0WebApplicationsMicrosoft.WebApplication.targets” was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

I have had this problem before when we migrated a “Website” to a “Web application project” at my last job. What i remembered from it was that you had to copy the file Microsoft.WebApplication.targets from it’s location on your dev machine to the build machine. Well fine. The problem in my case was that the file already was there. I looked around for maybe an hour or until i realized that the path on my build machine was:

C:Program FilesMSBuildMicrosoftVisual Studiov9.0WebApplicationsMicrosoft.WebApplication.targets

While on my dev machine it was:

C:Program FilesMSBuildMicrosoftVisualStudiov9.0WebApplicationsMicrosoft.WebApplication.targets

Notice the difference? Don’t you just love Microsoft sometimes .. just copy the path and remove the space from “Visual Studio” and it works.

That was actually the only major problem i had. Otherwise i used my brain and the articles i linked to and everything is happy puppy.

Thanks and good night.

Link: How to publish a web site with MSBuild, ASP.NET website Continuous Integration+Deployment using CruiseControl.NET, Subversion, MSBuild and Robocopy