列出以间隔返回特定字段的值 [英] List to return values in intervals for a specific field
问题描述
我正在用大量数据实现Telerik Chart.图表的x轴上的标签重叠.我已经解决了这个问题,但长期来看并不可靠.
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轴标签值来自DateString
字段
我实施的解决方案
- 最小和最大日期
DateString
"字段将始终返回. - 对于其余部分,返回工作日为星期一"的那些值
- The MIN and MAX Date
DateString
field will always return. - For the rest, return those values where weekday is 'Monday'
这是代码-
// 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.
我需要实施的解决方案
我正在思考但不知道如何实现的想法是-
An idea that I was thinking but don't know how to implement is-
- 最小和最大日期
DateString
"字段将始终返回. [与我现在正在做的事情相同] - 从MIN和MAX日期开始,以间隔为间隔返回8个值,而不管列表项的总数如何.
2.a.但是,如果列表计数小于或等于8,则返回所有值.
- The MIN and MAX Date
DateString
field will always return. [Same as what I am doing right now] - 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.
例如,如果在列表中,我有32个值.它应该在DateString字段中返回总计10个值,其余的将为空字符串.
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.
推荐答案
我建议这样:
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;
}
如果股票不是按时间顺序排列的,我会按日期按日期排序.Sort().
If the Stocks are not in chronological order, I'd call .Sort() by date on them.
您可能希望在代码中添加null和empty集合检查.
You'll probably like to add null and empty collection checking in your code.
这篇关于列出以间隔返回特定字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!