小编Fer*_*ndo的帖子

按任意时间间隔对DateTime进行分组

我有一个IEnumerable的项类定义如下:

public class Item {
    public DateTime Date { get; private set; }
    public decimal? Value { get; private set; }

    public Item(DateTime date, decimal? value) {
     Date = date;
     Value = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

这些项目处于特定的时间间隔(例如5分钟).我需要按日期对它们进行分组,但需要更改间隔.例如,如果项目按以下顺序排列:

2010-08-24 00:05
2010-08-24 00:10
2010-08-24 00:15
2010-08-24 00:20
2010-08-24 00:25
2010-08-24 00:30
Run Code Online (Sandbox Code Playgroud)

我希望将它们分成15分钟的间隔,结果应如下所示:

2010-08-24 00:15
2010-08-24 00:30
Run Code Online (Sandbox Code Playgroud)

间隔由另一个类提供,但我可以得到表示该间隔的毫秒数(例如,Interval.FromMinutes(5).GetMilliseconds()应该返回300000).问题是如何编写一个分组函数,允许我做这样的事情:data = items.GroupBy(t => GroupingFunction(t.DateTime, interval))并获得结果?

更新:间隔不一定以分钟为单位.它可能是几小时,几分钟甚至几天.

c# linq group-by

7
推荐指数
1
解决办法
5346
查看次数

如何在Rx.NET中以正确的方式约束并发

请观察以下代码段:

var result = await GetSource(1000).SelectMany(s => getResultAsync(s).ToObservable()).ToList();
Run Code Online (Sandbox Code Playgroud)

这段代码的问题getResultAsync是以无约束的方式同时运行.在某些情况下,这可能不是我们想要的.假设我想将其并发限制为最多10个并发调用.什么是Rx.NET方法呢?

我附上一个简单的控制台应用程序,演示了所描述问题的主题和我的蹩脚解决方案.

还有一些额外的代码,比如Stats类和人工随机睡眠.它们用于确保我真正获得并发执行,并且可以可靠地计算在此过程中达到的最大并发性.

该方法RunUnconstrained展示了天真的,无约束的运行.该方法RunConstrained显示了我的解决方案,这不是很优雅.理想情况下,我想通过简单地将专用Rx运算符应用于Monad来简化约束并发性.当然,不会牺牲性能.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;

namespace RxConstrainedConcurrency
{
    class Program
    {
        public class Stats
        {
            public int MaxConcurrentCount;
            public int CurConcurrentCount;
            public readonly object MaxConcurrentCountGuard = new object();
        }

        static void Main()
        {
            RunUnconstrained().GetAwaiter().GetResult();
            RunConstrained().GetAwaiter().GetResult();
        }
        static async Task RunUnconstrained()
        {
            await Run(AsyncOp);
        }
        static async Task RunConstrained()
        { …
Run Code Online (Sandbox Code Playgroud)

c# reactive-programming system.reactive

6
推荐指数
1
解决办法
1514
查看次数

评估软件最低要求

有没有办法评估软件的最低要求?我的意思是,例如,我怎样才能发现我的应用程序需要的最小 RAM 量?

谢谢!

memory minimum-requirements system-requirements

5
推荐指数
1
解决办法
1164
查看次数

缓存已编译的lambda表达式

我需要将一些信息作为lambda表达式传递给某些方法.基本上,它是一种向数据库查询添加信息的方法.一个简单的例子是:

companyData.GetAll(
   where => "SomeField = @SOMEFIELD",
   order => "Id",
   SOMEFIELD => new Parameter {DbType = DbType.String, Value = "some value"}
)
Run Code Online (Sandbox Code Playgroud)

它工作得很好,除了我需要调用LambdaExpression.Compile来获取参数对象这一事实,这会对性能产生很大的影响.

为了获得更快的结果,我参加了这个天真的缓存测试:

class ExpressionCache<T,U>
{

    private static ExpressionCache<T, U> instance;
    public static ExpressionCache<T, U> Instance
    {
        get
        {
            if (instance == null) {
                instance = new ExpressionCache<T, U>();
            }
            return instance;
        }
    }

    private ExpressionCache() { }

    private Dictionary<string, Func<T, U>> cache = new Dictionary<string, Func<T, U>>();

    public Func<T, U> Get(Expression<Func<T, U>> expression)
    {
        string key …
Run Code Online (Sandbox Code Playgroud)

c# performance lambda

4
推荐指数
2
解决办法
3591
查看次数

客户端验证asp.net mvc 3上的时间跨度

我需要以"hh:mm"(无秒)格式接收一些时间信息.该属性定义如下:

[DataType(DataType.Time), DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
public TimeSpan SomeTimeProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)

服务器端验证按预期工作.但是,我无法使客户端验证工作,因为没有生成客户端验证规则.

我怎样才能使它工作?

validation asp.net-mvc timespan custom-formatting

3
推荐指数
1
解决办法
3053
查看次数

StreamReader返回另一个char

我正在尝试使用StreamReader读取文件的内容,StreamReader接收FileStream.该文件中有一些空格(char 32),StreamReader将它们读为0(char 48).屏幕截图显示了FileStream缓冲区和StreamReader缓冲区.两者都有值32,但是当我调用Read()时,它返回48.我错过了什么吗?顺便说一句,代码在.NET Compact Framework下运行.

alt text http://www.freeimagehosting.net/uploads/9f72b61bbe.png

读取数据的代码:

public void Read() {
 using (StreamReader reader = new StreamReader(InputStream, Encoding.UTF8)) {
  foreach (var property in DataObject.EnumerateProperties()) {
   OffsetInfo offset = property.GetTextOffset();
   reader.BaseStream.Position = offset.Start - 1;
   StringBuilder builder = new StringBuilder(offset.Size);
   int count = 0;
   while (reader.Peek() >= 0 && count < offset.Size) {
    char c = (char)reader.Read();
    if ((int)c != 32 && c != '\r' && c != '\n')  {
     builder.Append(c);
     count++;
    } else {
     reader.BaseStream.Position++;
    }
   }
   property.SetValue(DataObject, …
Run Code Online (Sandbox Code Playgroud)

.net c# compact-framework stream streamreader

2
推荐指数
1
解决办法
1059
查看次数

用linq在聚合函数之间切换

我有两个看起来几乎相同的方法,除了Linq查询中使用的聚合函数.例如:

public IEnumerable<Item> DoStuff(IEnumerable<Item> someItems) {
    var items = someItems.GroupBy(i => i.Date).Select(p => new Item(p.Key, p.Sum(r => r.Value)));
    // ...
}

public IEnumerable<Item> DoOtherStuff(IEnumerable<Item> someItems) {
    var items = someItems.GroupBy(i => i.Date).Select(p => new Item(p.Key, p.Max(r => r.Value)));
    // ...
}
Run Code Online (Sandbox Code Playgroud)

Item是一个这样的类:

public class Item {
    public DateTime Date { get; private set; }
    public decimal Value { get; private set; }

    public Item(DateTime date, decimal value) {
     Date = date;
     Value = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

由于两种方法都做同样的事情,我想只有一种方法,我将IEnumerable和聚合函数(sum/max)作为参数传递.我怎样才能做到这一点?

c# linq aggregate-functions

2
推荐指数
1
解决办法
807
查看次数