How to install LINQ in cpython

Hi all,

I could add LINQ in ironpython like this

clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

it doesn’t work in cpython.

I also installed the following package

pip install python-linq

but when I import it it’s not recognised . Ideas ?

Hello, I’m not the most alert on these aspects but try maybe that

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
import System

OUT = dir(System.Linq)

Cordially
christian.stan

The following works with ironpython

import System
from System.Linq import *
lst = [1,2,3]
LinqList = lst.Where(lambda num : num!=2).ToList()
OUT = LinqList

but not in CPython
image

@salvatoredragotta ,

“Where” comes from numPy … you will need this library also

https://numpy.org/doc/stable/reference/generated/numpy.where.html

KR

Andreas

@salvatoredragotta unfortunately LINQ do not work with PythonNet (CPython3 engine)

2 Likes

IronPython 2 utilizes the “Where” functionality here as it’s converting the Python list to a .net enumberable (of some sort). As CPython is ‘closer to the bare metal’ it doesn’t handle .net interop as effectively, or natively - you’ll have to build your own interop method. As a result I recommend utilizing a Python native tool for such filters. Two examples of list comprehension and filtering are as follows:

lst = [1,2,3]
lstComp = [i for i in lst if i != 2]
lstFilt = filter(lambda x: x != 2, lst)
2 Likes