Highlighted
Contributor.
Contributor.
1614 views

Linq/Lambda

Jump to solution

How would one convert this linq/lambda expression to .net cobol

return Enumerable.Range(0, reader.FieldCount)
                 .ToDictionary(
                     i => reader.GetName(i),
                     i => reader.GetValue(i));
What I have so far is invoke type Enumerable::Range(0, reader::FieldCount)::ToDictionary(
I do not know how to covert the lambda expressions to .net cobol or is it possible?.
0 Likes
1 Solution

Accepted Solutions
Highlighted
Contributor.
Contributor.

RE: Linq/Lambda

Jump to solution
set dict to type Enumerable::Range(0, rdr::FieldCount)::ToDictionary
(
delegate using i as binary-long returning ret as string set ret to rdr::GetName(i) end-delegate
delegate using i2 as binary-long returning ret2 as string set ret2 to rdr::GetValue(i2) end-delegate
)

View solution in original post

0 Likes
7 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Linq/Lambda

Jump to solution
It should be possible to use anonymous methods in place of the lambda expressions (which are really just an alternative, more streamlined way of expressing C# anonymous methods). Something like:

set return-value to type Enumerable::Range(0, reader::FieldCount)::ToDictionary
(
delegate (i as binary-long) returning ret as string
set ret to reader::GetName(i)
end-delegate
delegate (i as binary-long) returning ret as binary-long (?)
set ret to reader::GetValue(i)
end-delegate
)
0 Likes
Highlighted
Contributor.
Contributor.

RE: Linq/Lambda

Jump to solution
Cobol was not happy with the syntax of the delegate. Here is the complete code that I am attempting to use it with.

method-id btnOK_Click (sender as object e as type System.EventArgs) final private.
local-storage section.
01 dict type Dictionary[string, string] value new Type Dictionary[string, string].
01 ret string.
01 i binary-long.

perform using conn as type MySqlConnection = new MySqlConnection(type Connection::GetConnection)
invoke conn::Open()
perform using cmd as type MySqlCommand = new MySqlCommand("GetUser", conn)
set cmd::CommandType to type CommandType::StoredProcedure
invoke cmd::Parameters::AddWithValue("@username", tbUserName::Text)
invoke cmd::Parameters::AddWithValue("@password", tbPassword::Text)
perform using rdr as type MySqlDataReader = cmd::ExecuteReaderExtended
perform until not rdr::Read()
set dict to type Enumerable::Range(0, rdr::FieldCount)::ToDictionary
(
delegate i returning ret as string
set ret to rdr::GetName(i)
end-delegate

delegate i returning ret as binary-long
set ret to rdr::GetValue(i)
end-delegate
)
end-perform
end-perform
end-perform
end method.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Linq/Lambda

Jump to solution
You don't have parenthesis around the arguments to the delegates:

delegate (I) returning ret as string
set ret to rdr::GetName(i)
end-delegate
0 Likes
Highlighted
Contributor.
Contributor.

RE: Linq/Lambda

Jump to solution
when I use delegate (I) returning ret... I am getting unrecognized verb at (I) and also and error at ToDictionary saying it has no visible to which generic parameter types can be inferred.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Linq/Lambda

Jump to solution
The params used in an anonymous delegate need to specified as new parameters using AS, etc. You are passing an existing data item in working-storage.
The following works for me:
delegate(ind as binary-long) returning delret as string
set ret to rdr::GetName(ind)
end-delegate
delegate(ind2 as binary-long) returning delret2 as binary-long
set ret to rdr::GetValue(ind2)
end-delegate
0 Likes
Highlighted
Contributor.
Contributor.

RE: Linq/Lambda

Jump to solution
declare something = type Enumerable::Range(0, rdr::FieldCount)::ToDictionary
(
delegate using i as binary-long returning ret as string set ret to rdr::GetName(i) end-delegate
delegate using i as binary-long returning ret as string set ret to rdr::GetValue(i) end-delegate
)

I figured it out .
0 Likes
Highlighted
Contributor.
Contributor.

RE: Linq/Lambda

Jump to solution
set dict to type Enumerable::Range(0, rdr::FieldCount)::ToDictionary
(
delegate using i as binary-long returning ret as string set ret to rdr::GetName(i) end-delegate
delegate using i2 as binary-long returning ret2 as string set ret2 to rdr::GetValue(i2) end-delegate
)

View solution in original post

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.