Question

I have an user table called zeekuser in which I am storing Time Zone information related to user. while retrieving I using below code,

var v = (from s in dc.UserWebSites
                             join zk in dc.ZeekUsers on s.aspnet_User.UserId equals zk.UserId
                             where s.aspnet_User.LoweredUserName.Equals(strUsername.ToLower())
                             select new UserWebSiteInfo
                             {
                                 CreateDt = TimeZoneInfo.ConvertTimeFromUtc(s.CreateDt, TimeZoneInfo.FindSystemTimeZoneById(zk.TimeZoneID)),
                                 LastUpdateDt = TimeZoneInfo.ConvertTimeFromUtc(s.LastUpdate, TimeZoneInfo.FindSystemTimeZoneById(zk.TimeZoneID)),
                                 LogoImage = s.LogoImage,
                                 Nickname = s.Nickname,
                                 Title1 = s.Title1,
                                 Title2 = s.Title2,
                                 SiteID = s.SiteID.ToString(),
                                 TemplateID = s.TemplateID.ToString(),
                                 TemplateName = s.WebSiteTemplate.ThemeName,
                                 IsActive = s.IsActive,
                                 IsRedirect = s.IsRedirect,
                                 RedirectURL = s.RedirectURL,
                                 UPID = s.UPID.ToString(),
                                 UserId = s.aspnet_User.UserId.ToString(),
                                 Username = s.aspnet_User.UserName,
                                 UserProductName = s.UserProductDetail.Nickname,
                                 PageCount = s.UserWebSitePages.Count(),
                                 AuthorName = s.AuthorName,
                                 AuthorURL = s.AuthorURL
                             }).OrderByDescending(y => y.LastUpdateDt);
                    info = v.ToList();

but I am getting below error

Method 'System.TimeZoneInfo FindSystemTimeZoneById(System.String)' has no supported translation to SQL.

Each different user can have different time zones. How can I resolve this?

Was it helpful?

Solution

I doubt that you can do that within the query that's sent to SQL Server. Instead, use AsEnumerable to change context to perform the final step on the .NET side:

var query = from s in dc.UserWebSites
            join zk in dc.ZeekUsers on s.aspnet_User.UserId equals zk.UserId
            where s.aspnet_User.LoweredUserName.Equals(strUsername.ToLower())
            orderby s.LastUpdate descending
            select new { Site = s, ZoneId = zk.TimeZoneID };

// Do the rest of the query in-process, so we can use time zones.
var results = (from pair in query.AsEnumerable()
               let site = pair.Site
               let zone = TimeZoneInfo.FindSystemTimeZoneById(pair.ZoneId)
               select new UserWebSiteInfo {
                   // Select all your properties here
               }).ToList();

This is assuming that there's not much more information in UserWebSites than you'll be using to construct the UserWebSiteInfo; if there is a load of information which is irrelevant, you should select the relevant parts explicitly in your initial db-side query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top