A list to return values ​​in intervals for a specific field

I use Telerik Chart with huge data. Labels on the x-axis of the chart overlap. I overcame this problem, but for a long time is not reliable.

These are the fields in which there are:

FieldName DataType Date DATETIME DateString STRING Unit DOUBLE Price DOUBLE 

X-Axis Label Value DateString from DateString

Solution i implemented

  • The MIN and MAX Date DateString will always be returned.
  • For the rest, return the values ​​where the day of the week is Monday

Here is the code -

 // Get min and max Date DateTime minDate = DateTime.Now; DateTime maxDate = DateTime.Now; if (dtGas.Rows.Count > 0) { minDate = Convert.ToDateTime(dtGas.Compute("MIN([Date])", "")); maxDate = Convert.ToDateTime(dtGas.Compute("MAX([Date])", "")); } // Group by 'Date' and 'DateString' | 'SUM' of Unit and 'Price' var qGas = from x in dtGas.AsEnumerable() group x by new { Date = x.Field<DateTime>("Date"), DateString = x.Field<string>("DateString") } into egroup let isOne = egroup.Key.Date.DayOfWeek.ToString() == "Monday" select new { Date = egroup.Key.Date, DateString = minDate == egroup.Key.Date ? ( egroup.Key.DateString ) : ( maxDate == egroup.Key.Date ? ( egroup.Key.DateString ) : ( (isOne) ? ( egroup.Key.DateString ) : (" ") ) ), Unit = egroup.Sum(r => r.Field<double>("Unit")), Price = egroup.Sum(r => r.Field<double>("Price")), }; 

This solution helps to return not all values, but some of them. Therefore, avoiding overlapping. But in the future, when data grows, even this solution will fail.

Decision. I need to implement

The idea that I was thinking about but don’t know how to implement is

  • The MIN and MAX Date DateString will always be returned. [Same as what I am doing now]
  • Returns 8 values ​​between MIN and MAX, regardless of the total number of list items.
    2.a But if the number of lists is less than or equal to 8, then return all values.

So, for example, if in the List I have 32 values. It should return me only 10 values ​​in the DateString field, and rest will be an empty string.

+5
source share
1 answer

I would suggest something like this:

  public static IList<Stock> GetSome(this IList<Stock> input) { var result = new List<Stock>(); if (input.Count < 8) { return input; } else { var i = 0; for (; i < input.Count; ++i) { if (i % 8 == 0) { result.Add(input[i]); } } if (i % 8 != 0) { result.Add(input.Last()); } } return result; } 

If the stocks are not in chronological order, I would call them .Sort () by date.

You probably want to add an empty and empty collection check to your code.

+1
source

Source: https://habr.com/ru/post/1235751/


All Articles