开发者

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

开发者 https://www.devze.com 2022-12-12 03:41 出处:网络
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:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号