Can you use LINQ types and extension methods in IronPython?

后端 未结 4 1952
迷失自我
迷失自我 2021-01-30 13:28

Is it possible to use the LINQ types and extension methods in IronPython?

If so how? And also is there often more pythonic to do the same thing?

4条回答
  •  庸人自扰
    2021-01-30 14:05

    I described a C# wrapper class around the LINQ extension methods to achieve a syntax similar to C#'s 'chained extension method' syntax in IronPython.

    The idea is to have a kind of decorator class around IEnumerable that simply calls the extension methods. Probably this wrapper class can be written just as well in IronPython, but I'm not as fluent in python yet :-)

    public class ToLinq : IEnumerable
    {
        private readonly IEnumerable _wrapped;
    
        public ToLinq(IEnumerable wrapped)
        {
           _wrapped = wrapped;
        }
    
        public ToLinq Where(Func predicate)
        {
            return new ToLinq(_wrapped.Where(predicate));
        }
    
    
        // ... similar methods for other operators like Select, Count, Any, ...
    
    }
    

    This allows for a syntax similar to this:

    johns = ToLinq[Customer](customers)\
              .Where(lambda c: c.Name.StartsWith("John"))\
              .Select(lambda c: c.Name)
    

    Disclaimer: this is something I tried as a learning excercise, I haven't used this in a real-world project.

提交回复
热议问题