List to return values in intervals for a specific field

戏子无情 提交于 2019-12-12 10:53:35

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/33664962/list-to-return-values-in-intervals-for-a-specific-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!