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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:40:23+00:00 2026-06-04T03:40:23+00:00

Let say we have two arrays: DateTime[] wDates = new DateTime[20000]; double[] wValues =

  • 0

Let say we have two arrays:

DateTime[] wDates = new DateTime[20000];
double[] wValues = new double[20000];

Those two arrays are both sequentially ordered, that is given an int i, wValues[i] is for the date wDates[i].

Let us say we need to get the average value of wValues where the month of the date is January

using a standard loop this would be:

double wAvg = 0.0;
int wDataCount = 0;
for (int i=0; i < 20000; i++)
  if (wDates[i].Month == 1)
  {
    wAvg += wValues[i];
    wDataCount++;
  }

if (wDataCount > 0)
  wAvg /= wDataCount;

I am wondering how to do this in LINQ?
I could create a struct/class DateDouble that contains both values and then do something like:

List<DateDouble> wListData = new List<DateDouble>();
Add the items...
double wAvg = (from d in wListData
               where d.Date.Month == 1
               select d.Value).Average();

but creating thousands of DateDouble object would be a big memory overhead when done dozens of millions time a day. The same would happen with temporary objects and trying to use ‘index’ and joining the index on the arrays would create awful performances.

Is there a better way to achieve this in LINQ?

Thanks,
MM

  • 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-06-04T03:40:24+00:00Added an answer on June 4, 2026 at 3:40 am

    There is an overloaded version of IEnumerable.Where() extension method which also considers the index in the predicate.

    double average = wValues.Where((d, i) => wDates[i].Month == 1).Average();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
Let's say I have two arrays: int[] array1 = new int[2000000]; int[] array2 =
Let's say I have these two arrays: var array1 = new[] {"A", "B", "C"};
Let's say I have two arrays: a = [1,2,3] b = [1,2] I want
let's say I have two arrays like so: $array1 = array('A' => array( 'B'
Let's say I have two Arrays in Ruby, containing user IDs. These are the
Let's say you have two arrays of arrays with the same structure but different
Anyone ever come up to this problem? Let's say you have two arrays like
Let's say we have a two dimensional arrays, arr[N][N] , where N is a
Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285};

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.