How to convert linq results to HashSet or HashedSet

前端 未结 9 765
闹比i
闹比i 2020-11-28 05:17

I have a property on a class that is an ISet. I\'m trying to get the results of a linq query into that property, but can\'t figure out how to do so.

Basically, look

9条回答
  •  忘掉有多难
    2020-11-28 05:58

    If you need just readonly access to the set and the source is a parameter to your method, then I would go with

    public static ISet EnsureSet(this IEnumerable source)
    {
        ISet result = source as ISet;
        if (result != null)
            return result;
        return new HashSet(source);
    }
    

    The reason is, that the users may call your method with the ISet already so you do not need to create the copy.

提交回复
热议问题