English 中文(简体)
Problem getting GUID string value in Linq-To-Entity query
原标题:

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();
最佳回答

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();
问题回答

暂无回答




相关问题
Problem getting GUID string value in Linq-To-Entity query

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....

Sequential Guid Generator

Is there any way to get the functionality of the Sql Server 2005+ Sequential Guid generator without inserting records to read it back on round trip or invoking a native win dll call? I saw someone ...

.NET: Blog post on GUID?

I once read a blog post by a dev at Microsoft about the anatomy--what individual pieces of data are combined--of a System.Guid. Does anyone have the link? I m not finding it by googling.

Simple proof that GUID is not unique [closed]

I d like to prove that a GUID is not unique in a simple test program. I expected the following code to run for hours, but it s not working. How can I make it work? BigInteger begin = new BigInteger((...

Subsonic Linq guid problem

The construtor Void .ctor(System.Guid, Int32) is not supported. this error occured with the following statements: var Test = from r in db.UserRoles join p in db.UserPermissions on new { r....

热门标签