Using ToString() in LINQ queries?

大兔子大兔子 提交于 2019-12-07 06:13:48

问题


I have writting a LINQ query to fill a listview but it useses the .ToString() method which apparetly is not allowed. When I use the below code I get the error message:

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

Is there a way to use the ToString() in LINQ or if that is not possible what is the solution to converting a DateTime to String in the query. Please knot that ReleaseDateName is a string and ReleaseDate is a DateTime

using (var db = new ReleaseInfo())
{
    lvReleaseInfo.DataSource = (from r in db.MediaReleases
                                join rn in db.ReleaseNames
                                on new { MediaReleaseID = r.MediaReleaseID, CultureCodeID } equals new { rn.MediaReleaseID, rn.CultureCodeID }
                                join plat in db.MediaPlatforms
                                on new { MediaPlatformID = r.MediaPlatformID, CultureCodeID } equals new { plat.MediaPlatformID, plat.CultureCodeID }
                                join pub in db.MediaPublishers
                                on new { MediaPublisherID = r.MediaPublisherID, CultureCodeID } equals new { pub.MediaPublisherID, pub.CultureCodeID }
                                join c in db.Countries
                                on new { CountryID = r.CountryID, CultureCodeID } equals new { c.CountryID, c.CultureCodeID }
                                join rd in db.ReleaseDates
                                on new { MediaReleaseID = r.MediaReleaseID, CultureCodeID } equals new { rd.MediaReleaseID, rd.CultureCodeID }
                                join a in db.AffiliateLinks
                                on new { MediaReleaseID = r.MediaReleaseID, CultureCodeID } equals new { a.MediaReleaseID, a.CultureCodeID }
                                where r.SectionID == SectionID
                                select new
                                {
                                    rn.ReleaseTitle,
                                    plat.MediaPlatformName,
                                    pub.MediaPublisherName,
                                    c.CountryName,
                                    ReleaseDate = (rd.ReleaseDate == null ? rd.ReleaseDateName : rd.ReleaseDate.ToString()),
                                    a.AffiliateLinkAddress
                                }).ToList();
    lvReleaseInfo.DataBind();
}

回答1:


Since you are materializing your query to list anyway, you could do the conversion on the .NET side, rather than in the RDBMS, like this:

...
select new {
   rn.ReleaseTitle,
   plat.MediaPlatformName,
   pub.MediaPublisherName,
   c.CountryName,
   rd.ReleaseDateName,
   rd.ReleaseDate,
   a.AffiliateLinkAddress
}).AsEnumerable() // <<== This forces the following Select to operate in memory
.Select(t => new {
   t.ReleaseTitle,
   t.MediaPlatformName,
   t.MediaPublisherName,
   t.CountryName,
   ReleaseDate = t.ReleaseDateName ?? t.ReleaseDate.ToString()
   t.AffiliateLinkAddress        
}).ToList();

Since the ToString() is called on an element from IEnumerable<T>, it will no longer fail. Also note the use of ?? operator in place of a null-checking ? : conditional.




回答2:


The problem is that you can't call ToString() on a field until it's been deserialized. So, rather than trying to call ToString() in the query, simply do it on the results afterwards.

In the database the value you're operating on has no notion of ToString() which is why you get the error. The query may look and feel like C# code but keep in mind that under the covers that is being transformed to a SQL query like any other. After you get the list back you can write a very simple LINQ query to solve the problem.



来源:https://stackoverflow.com/questions/16533274/using-tostring-in-linq-queries

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