C# Linq SortedList 过滤到 SortedList

发布于 2024-10-26 05:16:22 字数 722 浏览 0 评论 0原文

我有一些代码,其中我正在做一些奇怪的事情来从 SortedList 中获取信息并返回到另一个 SortedList 中。我执行 where 子句,然后必须将所有 KeyValuePair 单独放回到新的 SortedList 中。

不可能是最有效的,或者实际上是推荐的方法,但我似乎找不到更好的方法。

这是代码:

SortedList<DateTime, CalendarDay> most_days = 
                                new SortedList<DateTime, CalendarDay>();
List<KeyValuePair<DateTime, CalendarDay>> days = this.all_days.Where (
                                  n => n.Value.IsRequested || n.Value.IsApproved
                                  ).ToList();
foreach (KeyValuePair<DateTime, CalendarDay> kvp in days)
    most_days.Add(kvp.Key, kvp.Value);

关于如何清理它的任何想法(正如他们所说,少即是多)?

谢谢,

乔纳森

I have got some code in which I am doing some weirdness to get information out of a SortedList and back into another SortedList. I do my where clause, then have to individually put all the KeyValuePairs back into a new SortedList.

This can't be the most efficient, or indeed the recommended, way of doing this, but I can't seem to find a better way.

Here is the code:

SortedList<DateTime, CalendarDay> most_days = 
                                new SortedList<DateTime, CalendarDay>();
List<KeyValuePair<DateTime, CalendarDay>> days = this.all_days.Where (
                                  n => n.Value.IsRequested || n.Value.IsApproved
                                  ).ToList();
foreach (KeyValuePair<DateTime, CalendarDay> kvp in days)
    most_days.Add(kvp.Key, kvp.Value);

Any ideas on how I can clean this up (less is more, as they say)?

Thanks,

Jonathan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-11-02 05:16:22

好吧,您当然可以删除 ToList 调用 - 这对您没有任何帮助。

您可以使调用代码更简单,如下所示:

var dictionary = allDays.Where(n => n.Value.IsRequested || n.Value.IsApproved)
                        .ToDictionary(x => x.Key, x => x.Value);
var mostDays = new SortedList<DateTime, CalendarDay>(dictionary);

...但这将构建一个中间 Dictionary<,>,因此效率很低。

另一种选择是您可以编写自己的 ToSortedList 扩展方法,例如

public static SortedList<TKey, TValue> ToSortedList<TSource, TKey, TValue>
    (this IEnumerable<TSource> source,
     Func<TSource, TKey> keySelector,
     Func<TSource, TValue> valueSelector)
{
    // TODO: Argument validation
    var ret = new SortedList<TKey, TValue>();
    foreach (var element in source)
    {
        ret.Add(keySelector(element), valueSelector(element));
    }
    return ret;
}

然后调用代码将是:

var mostDays = allDays.Where(n => n.Value.IsRequested || n.Value.IsApproved)
                      .ToSortedList(x => x.Key, x => x.Value);

我怀疑这应该相当有效,因为它总是会向 end 添加值构建期间列表的

(对于完整的工作,您需要添加接受自定义键比较器等的重载......请参阅ToDictionary。)

Well you could certainly remove the ToList call - that's not helping you at all.

You could make the calling code simpler like this:

var dictionary = allDays.Where(n => n.Value.IsRequested || n.Value.IsApproved)
                        .ToDictionary(x => x.Key, x => x.Value);
var mostDays = new SortedList<DateTime, CalendarDay>(dictionary);

... but that's going to build an intermediate Dictionary<,>, so it's hardly efficient.

Another option is that you could write your own ToSortedList extension method, e.g.

public static SortedList<TKey, TValue> ToSortedList<TSource, TKey, TValue>
    (this IEnumerable<TSource> source,
     Func<TSource, TKey> keySelector,
     Func<TSource, TValue> valueSelector)
{
    // TODO: Argument validation
    var ret = new SortedList<TKey, TValue>();
    foreach (var element in source)
    {
        ret.Add(keySelector(element), valueSelector(element));
    }
    return ret;
}

Then the calling code will just be:

var mostDays = allDays.Where(n => n.Value.IsRequested || n.Value.IsApproved)
                      .ToSortedList(x => x.Key, x => x.Value);

I suspect this should be reasonably efficient, as it will always be adding values to the end of the list during construction.

(For a complete job you'd want to add overloads accepting custom key comparers etc... see ToDictionary.)

青朷 2024-11-02 05:16:22

不是对您的问题的直接答案(抱歉!) - 更多关于这个问题的问题:

  • 您实际上需要输出为 SortedList 吗?
  • 或者,您能否在输出为 IEnumerable 且结果顺序正确的情况下生存?

如果您在创建 mostDays 集合后从未打算向其添加/插入更多项目,那么您显然可以使用 varmostDays = allDays 创建一个 IEnumerable .Where(n => n.Value.IsRequested || n.Value.IsApproved);

Not a direct answer to your question (sorry!) - more a question on the question:

  • Do you actually need the output to be a SortedList?
  • Or can you survive with the output being an IEnumerable where the results happen to be in the right order?

If you never intend to add/insert more items to mostDays collection after you've created it, then you could obviously just create an IEnumerable using var mostDays = allDays.Where(n => n.Value.IsRequested || n.Value.IsApproved);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文