Linq limitations

by Pascal Parent 12. April 2009 18:46

Read this article in your language IT | EN | DE | ES

I am currently writing a small website for one of my companies clients and decided that I should use LINQ-to-Entities and the ADO.NET Entities Framework to make my life easier and to learn a bit more about LINQ. You may recall a statement I did a few months ago on Twitter “ADO.NET Entity Framework sucks! Linq Rocks!” well I stand by that but it has some serious shortcomings, I would call it a bug but then it would be a documented bug. You cannot use any Entity.Field.ToString in a Linq block!

So the following code will fail:

using (Entities db = new Entities())
{
    var news = (
                   (
                       from N in db.NewsSet
                       where N.PublishedDate <= DateTime.Now
                       orderby N.PublishedDate descending
                       select new
                       {
                           Date = N.PublishedDate.ToString("dddd, dd MMMM yyyy"),
                           Headline = N.Headline,
                           Article = N.Content,
                           Author = N.Author,
                       })
                   ).First();

    this.litHeadline.Text = news.Headline.Trim();
    this.lblDate.Text = news.Date.Trim();
    this.litArticle.Text = news.Article.Trim();

}

The error is: System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

The line that will fail is Date = N.PublishedDate.ToString("dddd, dd MMMM yyyy") because I use ToString, which in my opinion is ridiculous. Why can I not format or manipulate the variable as I wish? Microsoft, please fix this or remove the Intellisence that allows me to do that. See be llow.

Linq.ToString

The actual code that works is here bellow:

using (Entities db = new Entities())
{
    var news = (
                   (
                       from N in db.NewsSet
                       where N.PublishedDate <= DateTime.Now
                       orderby N.PublishedDate descending
                       select new
                       {
                           Date = N.PublishedDate,
                           Headline = N.Headline,
                           Article = N.Content,
                           Author = N.Author,
                       })
                   ).First();

    this.litHeadline.Text = news.Headline.Trim();
    this.lblDate.Text = news.Date.ToString("dddd, dd MMMM yyyy");
    this.litArticle.Text = news.Article.Trim();

}

For more information Muhammad Mosa has a good article called Linq to Entities, what is not supported? I highly recommend that you read it along with Microsoft’s MSDN Supported and Unsupported Methods (Linq to Entities) 

Next Dynamic Linq…

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Tags:

General

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 The ASP.NET Guy