Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3432776
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:29:32+00:00 2026-05-18T07:29:32+00:00

It seem to me that this was hard… var MostRated = (from p in

  • 0

It seem to me that this was hard…

var MostRated = (from p in db.Posts
                                 let AverageRating = CalculateAverageRatingFromPost(p)
                                 where p.PostStatus == Convert.ToInt32(PostStatusEnum.Published.Value) && p.IsDeleted == false
                                 orderby p.PublishedDate descending
                                 select new
                                 {
                                     PostUrl = Common.PostUrl(p.Section.Name, p.Permalink),
                                     Title = Common.TrimString(p.Title, 50),
                                     Excerpt = Common.TrimString(p.Excerpt, 80),
                                     AverageRate =  CalculateAverageRating((from pr in db.Ratings
                                                    where pr.ObjectType == Convert.ToInt32(ObjectTypeEnum.Post.Value) && pr.ObjectID == p.ID
                                                    select pr).SingleOrDefault()),
                                     PublishedDate = new DateHelper().DateTimeInWords(p.PublishedDate.Value)
                                 }).OrderByDescending(o => o.AverageRate).Take(5).ToList();

and the other method is

private Decimal CalculateAverageRating(Rating pr)
        {
            if (pr != null)
            {
                Decimal Average = ((1 * (Decimal)pr.Star1) + (2 * (Decimal)pr.Star2) + (3 * (Decimal)pr.Star3) + (4 * (Decimal)pr.Star4) + (5 * (Decimal)pr.Star5)) / ((Decimal)pr.Star1 + (Decimal)pr.Star2 + (Decimal)pr.Star3 + (Decimal)pr.Star4 + (Decimal)pr.Star5);
                return Average;
            }
            else
            {
                return 0;
            }
        }

I get this run time error “no supported translation to SQL”.

What i want is to get lists of the posts, and do a small quick calculations of the rating and then sort those from highest to low and take 5 posts only.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:29:32+00:00Added an answer on May 18, 2026 at 7:29 am

    I haven’t tested this, but try defining the function as an Expression object.

    Expression<Rating, Decimal> CalculateAverageRating =
        pr => (Decimal)(
            pr == null ? 0 :
             (pr.Star1 + 2*pr.Star2 + 3*pr.Star3 + 4*pr.Star4 + 5*pr.Star5)
            /(pr.Star1 +   pr.Star2 +   pr.Star3 +   pr.Star4 +   pr.Star5));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's possible that this question has already been asked, but I can't seem to
This doesn't seem to work (compiler complains that Something 's getFoo() method doesn't implement
I can't seem to find anywhere that talks about doing this. Say I have
My project doesn't seem to be aware of this new constant that's apparently auto-generated
This is a super simple question that I just can't seem to find a
This may seem a trivial question, but it's one that's bothered me a lot
I never seem to get this right. I've got a method that returns a
I know that there are lot's of questons on this, but all seem to
OK so I have an xslt issue that I cannot seem to solve. This
I understand that this question may seem somewhat ungrounded, but if someone knows anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.