Python's list comprehension vs .NET LINQ

前端 未结 4 1923
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 09:17

The following simple LINQ code

string[] words = { \"hello\", \"wonderful\", \"linq\", \"beautiful\", \"world\" };

// Get only short words
var shortWords =
         


        
4条回答
  •  误落风尘
    2020-12-07 10:08

    Well, you need to distinguish between some different things:

    • LINQ standard query operators
    • LINQ query expressions in C#
    • LINQ query expressions in VB

    C# doesn't support as much in query expressions as VB does, but here's what it does support:

    • Projections (select x.foo)
    • Filtering (where x.bar > 5)
    • Joins (x join y on x.foo equals y.bar)
    • Group joins (x join y on x.foo equals y.bar into g)
    • Grouping (group x.foo by x.bar)
    • Ordering (orderby x.foo ascending, y.bar descending)
    • Intermediate variables (let tmp = x.foo)
    • Flattening (from x in y from z in x)

    I don't know how many of those are supported directly in Python's list comprehensions.

    Note that although LINQ to Objects deals with delegates, other query providers (e.g. LINQ to SQL) can deal in expression trees which describe the query instead of just presenting executable delegates. This allows the query to be translated into SQL (or other query languages) - again, I don't know whether Python supports that sort of thing or not. It's a significant part of LINQ though.

提交回复
热议问题