Problem getting GUID string value in Linq-To-Entity query

社会主义新天地 提交于 2019-11-28 10:25:42

问题


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();

回答1:


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();



回答2:


since my suggestion below doesn't work

look at this question and look at the answer, this might be the solution.

try this

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



来源:https://stackoverflow.com/questions/1765423/problem-getting-guid-string-value-in-linq-to-entity-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!