• Home
  • Functionality
  • Functions
  • Pricing
  • News
  • Blogs
Download
Login
Skip Navigation LinksBlogs > SimonOpelt > Blog
Follow timecockpit on Twitter

Using LINQ in IronPython 2.6

By Simon Opelt in Category: IronPython
Wednesday, March 31, 2010

Up until (and including) our beta 2 release time cockpit used IronPython 2.4 for executing scripts. When creating complex scripts we often had to iterate over collections and check for certain conditions or concatenate collections of strings. Whenever possible we try to solve such tasks using TCQL when selecting the source data from the data layer to pass most of the work on to the database.

But as soon as the task required to operate on a set of EntityObjects in memory we wanted to use LINQ because of our daily use of .NET 4. Several times we tried and only partially succeeded when IronPython was unable to bind certain types of generic functions and raised Microsoft.Scripting.ArgumentTypeException: Multiple targets could match. Most of these issues are resolved by the version bump to IronPython 2.6 and we are now able to write scripts like the following set of examples:

clr.AddReference("System.Core")
from TimeCockpit.Data import EntityObject
from System.Linq import Enumerable
from System import Func
from System import Int64
from System import String

# get all projects
projects = Context.Select("From P In Project Select P")

# filter (in memory) all projects with a name beginning with T
resultProjectsWithT = Enumerable.ToList[EntityObject](Enumerable.Where(projects, lambda p: p.ProjectName.StartsWith("T")))

# sort a collection
resultSorted = Enumerable.ToList[EntityObject](Enumerable.OrderBy(projects, lambda p: p.StartDate))

# pretty print a list of project names
print String.Join(", ", Enumerable.ToArray(Enumerable.Select(resultProjectsWithT, lambda p: p.ProjectName)))

# determine a minimum and maximum
func = Func[object, Int64](lambda p: p.StartDate.Ticks)
print DateTime(Enumerable.Min(projects, func)), DateTime(Enumerable.Max(projects, func))

Note that the generic type parameter EntityObject and Enumerable.ToList are only required because I want to easily show the data in a grid. The very last example shows one of the cases in which the runtime is not able to resolve all the generic types when not giving it a hint by wrapping the python lambda into a typed Func delegate.

I am sure that Python offers equally (or even more) elegant ways to handle such tasks. ;)

  • Facebook
  • Twitter
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Add diigo bookmark

Comments  4

  • Lainey 14 Jan

    I had no idea how to approach this before-now I'm lokecd and loaded.
  • Janaye 14 Jan

    And to think I was going to talk to seomnoe in person about this.
  • Bucky 15 Jan

    Is that rellay all there is to it because that'd be flabbergasting.
  • Debrah 15 Jan

    Deep tnhiking - adds a new dimension to it all.
Post a comment!
  1. Formatting options
       
     
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
     
     
     
       

Blogger

  • Alexander Huber (1)
  • Karin Huber (3)
  • Philipp Aumayr (10)
  • Rainer Stropek (39)
  • Simon Opelt (5)

Kategorien

  • .NET (7)
  • Azure (4)
  • IronPython (5)
  • Screencasts (12)
  • time cockpit (27)
About Us  |  Legal Notices  | © 2010 software architects gmbh. All rights reserved.