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 3312320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:58:52+00:00 2026-05-17T21:58:52+00:00

I’ve found several posts detailing how to perform a weighted average based on a

  • 0

I’ve found several posts detailing how to perform a weighted average based on a foreign key, but I have yet to find a solution that deals with my situation. Here it is:

I have two tables, table A and a table B many-to-many table linking them; nothing complicated:

TableA
{
     A_ID,
     Other stuff
}

TableB
{
     B_ID,
     Date
     Other stuff
}

LinkAtoB
{
     A_ID,
     B_ID
}

Now here comes the math part. I’m more or less trying to weight result from TableA based on the number of recent associations in Table B.

So if TableA has 4 associations in table with the following dates:

{10/23/2010, //3 days ago
10/19/2010,  //5 days ago
10/18/2010,  //6 days ago
9/13/2010}   //40ish days ago

So here is how I’d like to rank them:

I’d like to provide a recency threshold in days, I’ll use 7 days as an example:

So using the above data I would assign the following values:

{10/23/2010, //7-3 = 4
10/19/2010,  //7-5 = 2
10/18/2010,  //7-6 = 1
9/13/2010}   //40ish days ago

So the value of the weighted average for that particular TableA entry is then 7 / 3 = 2.33333.

Here is more or less what I have so far:

var k = from a in TableA
        group a by a.Select(x=>x.LinkAtoB.TableB)
                    .Where(x=>x.Date.CompareTo(DateTime.Now.AddDays(-7)) >= 0)
                    into g
        select g.Sum(x => DateTime.Now.Subtract(x.Date).Days) / 
        g.Sum(x => x.Length);

I think I’m close but I know I have the group part wrong. I think the other stuff should work. How do I fix my code to accomplish what I want?

  • 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-17T21:58:52+00:00Added an answer on May 17, 2026 at 9:58 pm

    Here you go! 🙂

    var k = (from b in TableB
            join bb in LinkAtoB on b.B_ID equals bb.B_ID into b_join
            from ab in b_join.DefaultIfEmpty()
            where b.B_DATE.CompareTo(DateTime.Now.AddDays(-7)) > 0
            select new {ab.A_ID, DaysAgo = (DateTime.Now - b.B_DATE).Days} into xx
            group xx by xx.A_ID into yy
            select new {yy.Key, Weighted = yy.Sum(x=> 7 - x.DaysAgo) / yy.Count()} into zz
            join a in TableA on zz.Key equals a.A_ID
            select new {a.A_ID, a.A_Other_Stuff, zz.Weighted}).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.