How can I implement NotOfType in LINQ that has a nice calling syntax?

前端 未结 7 1158
轻奢々
轻奢々 2020-12-11 00:24

I\'m trying to come up with an implementation for NotOfType, which has a readable call syntax. NotOfType should be the complement to OfType&l

7条回答
  •  醉梦人生
    2020-12-11 00:49

    If you're going to make a method for inference, you want to infer all the way. That requires an example of each type:

    public static class ExtMethods
    {
        public static IEnumerable NotOfType(this IEnumerable source)
        {
            return source.Where(t => !(t is U));
        }
          // helper method for type inference by example
        public static IEnumerable NotOfSameType(
          this IEnumerable source,
          U example)
        {
            return source.NotOfType();
        }
    }
    

    called by

    List items = new List() { 1, 1.0m, 1.0 };
    IEnumerable result = items.NotOfSameType(2);
    

提交回复
热议问题