Louis-Philippe Pinsonneault

.NET Addicted!

janvier 2009 - Posts

Silverlight Wishes card!

This year at RunAtServer Consulting we made our wishes card with Silverlight and DeepZoom!

Click the following link to explore our Silverlight Wishes card:
www.runatserver.com/wishes.aspx

French version here.

RunAtServer Consulting Silverlight Wishes card

Casting issues with Linq and Entity Framework

Recently i was working on a project where i was using Entity Framework as the data layer. It's the first project i'm working with it. In one of my query I wanted to output some fields merge in one fields like the following query:

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable GetListJoueursByClubId(int clubId)
{
    var query = from joueur in this.Joueurs
                where joueur.Clubs.Id == clubId
                orderby joueur.Clubs.Nom
                select new { Id = joueur.Id, NomComplet = joueur.Nom + ", " + joueur.Prenom
                                                          + " " + (joueur.Numero ?? 0) };
    return query; 
}
 
But I was always getting errors related to casting.
image
 
 
There was many solutions that could be applied. But one solution to solve the issue at the query level, it's to use a sub query. This way the first query will be executed in memory, and Linq will know the type of the object we are working with and Linq will be able to cast the object properly.
 
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable GetListJoueursByClubId(int clubId)
{
    var query = from joueur in this.Joueurs
                where joueur.Clubs.Id == clubId
                orderby joueur.Clubs.Nom
                select new { Id = joueur.Id, 
                             Nom = joueur.Nom, 
                             Prenom = joueur.Prenom, 
                             Numero = joueur.Numero
                            };

    return from joueur in query.ToList()
           select new { Id = joueur.Id, NomComplet = joueur.Nom + ", " + joueur.Prenom 
                                                     + " " + (joueur.Numero ?? 0) };
}
 
I hope this will help you, if you face this kind of issue.
 
 
Regards,
Louis-Philippe
 
Happy New Year!

I want to take some times to wish all of you an happy new year and i hope this new year will bright health and success to everyone.

For my part, I don`t take any resolution but I prefer to fix myself some objective. Some of those objective are to gain new certification (WCF 3.5), upgrade my current MCPD certification for ASP.NET 3.5 and work on some Silverlight project.

Happy New Year!

Louis-Philippe