logo
down
shadow

LINQ from SortedList C#


LINQ from SortedList C#

By : Can Savcı
Date : October 17 2020, 11:12 PM
this one helps. Original SortedList class is part of Collection. Generics introduces SortedList but if you have old code, then it wont be using that.
The easiest is to use the Cast method or OfType extension method that is made available for SortedList. Those methods will give you IEnumerable list that you can apply generic Linq operations. Make sure you include using System.Linq; in the beginning then you should be able to do like below:
code :


Share : facebook icon twitter icon
LINQ into SortedList

LINQ into SortedList


By : Rohith AP
Date : March 29 2020, 07:55 AM
it should still fix some issue Writing a binary search on your own can be tough.
Fortunately, Microsoft already wrote a pretty robust one: Array.BinarySearch. This is, in fact, the method that SortedList.IndexOfKey uses internally. Only problem is, it takes a T[] argument, instead of any IList (like SortedList.Keys).
code :
public static int BinarySearch<T>(this IList<T> list, int index, int length, T value, IComparer<T> comparer) {
    if (list == null)
        throw new ArgumentNullException("list");
    else if (index < 0 || length < 0)
        throw new ArgumentOutOfRangeException((index < 0) ? "index" : "length");
    else if (list.Count - index < length)
        throw new ArgumentException();

    int lower = index;
    int upper = (index + length) - 1;

    while (lower <= upper) {
        int adjustedIndex = lower + ((upper - lower) >> 1);
        int comparison = comparer.Compare(list[adjustedIndex], value);
        if (comparison == 0)
            return adjustedIndex;
        else if (comparison < 0)
            lower = adjustedIndex + 1;
        else
            upper = adjustedIndex - 1;
    }

    return ~lower;
}

public static int BinarySearch<T>(this IList<T> list, T value, IComparer<T> comparer) {
    return list.BinarySearch(0, list.Count, value, comparer);
}

public static int BinarySearch<T>(this IList<T> list, T value) where T : IComparable<T> {
    return list.BinarySearch(value, Comparer<T>.Default);
}
int index = list.Keys.BinarySearch(i);
if (index < 0)
    index = ~index;
var item = index < list.Count ? list[list.Keys[index]] : null;
return item;
C# Linq SortedList Filtering into SortedList

C# Linq SortedList Filtering into SortedList


By : Daa Ntje
Date : March 29 2020, 07:55 AM
Hope that helps Well you could certainly remove the ToList call - that's not helping you at all.
You could make the calling code simpler like this:
code :
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);
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);
SortedSet / SortedList with better LINQ performance?

SortedSet / SortedList with better LINQ performance?


By : SittingPilgrim
Date : March 29 2020, 07:55 AM
seems to work fine The problem for LINQ is that it can't know the sorted set is ordered exactly the same way as the query expects. Since any ordered collection can be created with an IComparer / IComparable / Comparison, there is no knowing that > 500000 actually makes sense. Maybe you've got a custom method on the comparer that first sorts by Odd/Even, then by number. In which case the order would be completely messed up and O(n) is required in all cases.
So to be on the safe side, LINQ will need to iterate through all elements in the Collection, even when it is sorted in some way. The default .Where implementation does not contain an optimization for ordered collections.
SortedList and Linq

SortedList and Linq


By : Haziq
Date : March 29 2020, 07:55 AM
around this issue Read the documentation...
From the documentation on the Values property:
C# Linq return SortedList

C# Linq return SortedList


By : Juan José
Date : March 29 2020, 07:55 AM
it fixes the issue The simplest way would probably be to create a dictionary using ToDictionary, and then call the SortedList(dictionary) constructor. Alternatively, add your own extension method:
shadow
Privacy Policy - Terms - Contact Us © bighow.org