Highlighted
Absent Member.
Absent Member.
3202 views

How to use Linq ??

Jump to solution

Hi 🙂
I'm codding some test here, and, I see it's possible to use linq functions with VCobol, but, I don't know how to use that.

http://i.imgur.com/4v79axn.png

how I can use the select linq function, or, how I can make lambda expressions with Visual Cobol ?

Tags (1)
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: How to use Linq ??

Jump to solution

There is currently no support for lambda expressions, as in C#, but we do support anonymous methods.  These are specified as a code block delimited by keywords DELEGATE and END-DELEGATE and containing a procedure division header and a code block.

I'm going to paste in a program showing how you can use method invocation syntax to access LINQ-type extension methods (in this case the Where method in System.Linq.Enumerable).  The program also shows the use of some experimental LINQ style syntax, which is not currently functional, but may included in future versions of the product.

Note that this program uses some of our new syntax for portable collection classes (i.e. portable between the .NET and JVM worlds), but these statements (CREATE, WRITE etc.) can easily be replaced by standard construction expression or method invocations.

     $set sourceformat"Variable"

     $set ilref"System.Core.dll"

     $set ilusing"System"

     $set ilusing"System.Collections.Generic"

      program-id. Program2 as "Linq.Program2".

      procedure division.

          declare myList as list[binary-long]

          create myList

          write myList from 10

          write myList from 9

          write myList from 11

          write myList from 999

          declare func as type Func[binary-long condition-value] = method type a::Select

          declare myEnumerable as type IEnumerable[binary-long]

          display "Using type inference and delegate instance"

     *> invoke 'Where' method, explicitly passing a parameter of type

     *> Func[binary-long, condition-value]

     *> In this case the compiler infers the generic parameter type

          set myEnumerable to myList::Where(func)

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

          display "Using type inference and method group"

     *> Here the parameter passed is a 'method group', which is implicitly converted

     *> into the correct delegate type (Func[binary-long, condition-value)

          set myEnumerable to myList::Where(method type a::Select)

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

          display "Using type inference and anonymous method"

     *> Here the parameter is specified as an anonymous method, i.e. the code

     *> between the delegate/end-delegate delimiters.

          set myEnumerable to myList::Where

                (delegate using i2 as binary-long returning ret as condition-value

                     if i2 > 10

                         set ret to true

                     else

                         set ret to false

                     end-if

                 end-delegate

                )

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

     $if 0 = 1

          display "Using LINQ syntax"

     *> This last example does the same thing, but using experimental LINQ-like

     *> syntax.  Currently this is not documented and not guaranteed...

          set myEnumerable to select i from mylist where i > 10

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

     $end

          goback.

      end program.

      class-id a.

      method-id Select static.

      procedure division using by value i as binary-long

                                returning ret as condition-value.

          if i > 10

              set ret to true

          else

              set ret to false

          end-if

      end method.

      end class.

View solution in original post

0 Likes
6 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: How to use Linq ??

Jump to solution

There is currently no support for lambda expressions, as in C#, but we do support anonymous methods.  These are specified as a code block delimited by keywords DELEGATE and END-DELEGATE and containing a procedure division header and a code block.

I'm going to paste in a program showing how you can use method invocation syntax to access LINQ-type extension methods (in this case the Where method in System.Linq.Enumerable).  The program also shows the use of some experimental LINQ style syntax, which is not currently functional, but may included in future versions of the product.

Note that this program uses some of our new syntax for portable collection classes (i.e. portable between the .NET and JVM worlds), but these statements (CREATE, WRITE etc.) can easily be replaced by standard construction expression or method invocations.

     $set sourceformat"Variable"

     $set ilref"System.Core.dll"

     $set ilusing"System"

     $set ilusing"System.Collections.Generic"

      program-id. Program2 as "Linq.Program2".

      procedure division.

          declare myList as list[binary-long]

          create myList

          write myList from 10

          write myList from 9

          write myList from 11

          write myList from 999

          declare func as type Func[binary-long condition-value] = method type a::Select

          declare myEnumerable as type IEnumerable[binary-long]

          display "Using type inference and delegate instance"

     *> invoke 'Where' method, explicitly passing a parameter of type

     *> Func[binary-long, condition-value]

     *> In this case the compiler infers the generic parameter type

          set myEnumerable to myList::Where(func)

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

          display "Using type inference and method group"

     *> Here the parameter passed is a 'method group', which is implicitly converted

     *> into the correct delegate type (Func[binary-long, condition-value)

          set myEnumerable to myList::Where(method type a::Select)

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

          display "Using type inference and anonymous method"

     *> Here the parameter is specified as an anonymous method, i.e. the code

     *> between the delegate/end-delegate delimiters.

          set myEnumerable to myList::Where

                (delegate using i2 as binary-long returning ret as condition-value

                     if i2 > 10

                         set ret to true

                     else

                         set ret to false

                     end-if

                 end-delegate

                )

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

     $if 0 = 1

          display "Using LINQ syntax"

     *> This last example does the same thing, but using experimental LINQ-like

     *> syntax.  Currently this is not documented and not guaranteed...

          set myEnumerable to select i from mylist where i > 10

          perform varying i1 as binary-long through myEnumerable

              display i1

          end-perform

     $end

          goback.

      end program.

      class-id a.

      method-id Select static.

      procedure division using by value i as binary-long

                                returning ret as condition-value.

          if i > 10

              set ret to true

          else

              set ret to false

          end-if

      end method.

      end class.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: How to use Linq ??

Jump to solution

The linq style not work 😞

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: How to use Linq ??

Jump to solution

Can you please elaborate?

What exactly is not working?

Are you using the Visual COBOL 2.2 product?

Thanks

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: How to use Linq ??

Jump to solution

$if 0 = 1

         display "Using LINQ syntax"

    *> This last example does the same thing, but using experimental LINQ-like

    *> syntax.  Currently this is not documented and not guaranteed...

         set myEnumerable to select i from mylist where i > 10

         perform varying i1 as binary-long through myEnumerable

             display i1

         end-perform

    $end

That, nor work...

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: How to use Linq ??

Jump to solution

Sorry, it would probably have been better to have omitted the linq-style syntax (which may well not be working in the currently released products).  For the moment, you'll have to use the other alternatives, but I wanted to see if there was interest in 'productising ' this feature.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: How to use Linq ??

Jump to solution

The linq is so good, why not put the Linq ?

www.google.com.br/search

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.