1

I am trying to write a GUID value to a string in a linq select. The code can be seen below (where c.ID is GUID), but I get the following error:

Unable to cast the type 'System.Guid' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

var media = (
                from media in Current.Context.MediaSet
                orderby media.CreatedDate
                select new Item
                {
                    Link = "~/Media.aspx?id=" + media.ID,
                    Text = "Media",
                    Time = media.CreatedDate
                }
            ).ToList();
Fischer
  • 246
  • 1
  • 3
  • 11

1 Answers1

2

One way would be to break apart the query into L2E and L2O:

var q = from media in Current.Context.MediaSet
        orderby media.CreatedDate
        select new
        {
            Id = media.ID,
            Time = media.CreatedTime
        };
var media = (
                from m in q.AsEnumerable()
                select new Item
                {
                    Link = "~/Media.aspx?id=" + q.Id.ToString,
                    Text = "Media",
                    Time = q.Time
                }
            ).ToList();
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • Thanks. This worked like a charm. To bad they dont have a simplier way to get the string value of the GUID though. – Fischer Nov 20 '09 at 15:54
  • 1
    This worked great! Thanks for the insight. I like this much better than having to resort to [SqlFunctions.StringConvert](http://stackoverflow.com/questions/1228318/linq-int-to-string), which doesn't work for anything other than a number. – Matt Penner Aug 01 '11 at 18:32