Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List to return values in intervals for a specific field

Tags:

c#

list

telerik

I am implementing Telerik Chart with a huge data. The labels on x-axis of the chart are overlapping. I have overcome this problem but it is not reliable for long run.

These are the fields List have:

FieldName                DataType
Date                     DATETIME
DateString               STRING
Unit                     DOUBLE
Price                    DOUBLE

X-Axis label value comes from DateString field

Solution I implemented

  1. The MIN and MAX Date DateString field will always return.
  2. For the rest, return those values where weekday is 'Monday'

Here is 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. Hence avoiding overlapping. But in future as data grows, even this solution will fail.

Solution I need to implement

An idea that I was thinking but don't know how to implement is-

  1. The MIN and MAX Date DateString field will always return. [Same as what I am doing right now]
  2. Return 8 values in intervals from MIN and MAX date regardless of total count of List Item.
    2.a. But if List count is less than or equals to 8 then return all values.

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

like image 204
soccer7 Avatar asked Nov 10 '22 01:11

soccer7


1 Answers

I'd 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'd call .Sort() by date on them.

You'll probably like to add null and empty collection checking in your code.

like image 146
Piotr Falkowski Avatar answered Nov 14 '22 23:11

Piotr Falkowski