question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C# System.Linq.Expression needs special treatment

See original GitHub issue

Using 6.8.0

First a difficult one – LINQ. Converting a string compare to

 String.Compare(a,b,true) == 0

is rejected by LINQ and is wrong since the case sensitivity of LINQ is a function of the database, not the VB program. Unfortunately, you don’t necessarily know it’s a LINQ to SQL expression so it’s not clear what is right. Perhaps an option or a flag?

I had an expression

   req.devID.Split("/"c)(0)  

the (0) was not converted to [0] as it should’ve been. I’ll change the source to .First() to avoid this.

RIght now I put the conversion on hold as I try to clean up async-- VB is far more lenient and the legacy code hasn’t been fully converted though it works. Not much you can do about that.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
BobFrankstoncommented, Jun 3, 2019

Just an FYI I looked into how to return a null task object to keep VB happy

Return Task.FromResult(Of Object)(Nothing)

Bob Frankston

https://Frankston.com https://Frankston.com

From: Bob Frankston bob19-0501@bobf.frankston.com Sent: Sunday, June 2, 2019 17:31 To: ‘icsharpcode/CodeConverter’ reply@reply.github.com; ‘icsharpcode/CodeConverter’ CodeConverter@noreply.github.com Cc: ‘Bob Frankston’ Github@bob.ma; ‘Author’ author@noreply.github.com Subject: RE: [icsharpcode/CodeConverter] VB -> C# LINQ Etc. (#316)

LINQ expressions are parsed with no semantics. It’s 100% up to the particular implementation to decide, at runtime, what to do. In fact some databases are case sensitive and some are not. So the only correct conversion is to leave it as-is and, fortunately, that means we don’t need pragmas … except … that you don’t necessarily know if a particular delegate will be used in LINQ or will be used as code. I’d have to dig deeper to know all the rules.

BTW, key insight on async. I think I get away with it in VB because I’m invoking async methods in non-async context so VB doesn’t care.

Also to declare an async method parameter I had to convert “foo of action(of object)” to “foo of func(of object,task)” rather than an async keyword as I do in TypeScript (I have far more experience with async in TS than in C#). I did have trouble in VB with

a.ForEach(func(a) await handler(a0)

VB gave me now way to make the containing sub async so I had to kludge it and created a handler that recursed down each entry in A so that there are just simple async invocations. There is probably a far better way of doing it but whatever I can make work. Caveat – I haven’t tested the new code in production yet.

Private Async Function SendDeviceStatusStepHack(wsdrs As IEnumerable(Of WSDevReport), SendJSON As Func(Of Object, Task)) As Task

    If wsdrs.Count = 0 Then Return

    Await SendJSON(wsdrs(0))

    Await SendDeviceStatusStepHack(wsdrs.Skip(1), SendJSON)

End Function



Friend Async Function SendDeviceStatus2(dev As iiDevice, SendJSON As Func(Of Object, Task)) As Task

        Dim wsds = New WSDevicesReport(dev)  ' Get matching devices

        '            wsds.devices.ForEach(Async Sub(wsdr) Await SendJSON(wsdr))

        Await SendDeviceStatusStepHack(wsds.devices, SendJSON)

End Function

This was the version before I did the conversion to async

Friend Sub SendDeviceStatus(dev As iiDevice, SendJSON As Action(Of Object))

        Dim wsds = New WSDevicesReport(dev)  ' Get matching devices

        wsds.devices.ForEach(Sub(wsdr) SendJSON(wsdr))

End Sub

Bob Frankston

https://Frankston.com https://Frankston.com

From: GrahamTheCoder <notifications@github.com mailto:notifications@github.com > Sent: Sunday, June 2, 2019 17:10 To: icsharpcode/CodeConverter <CodeConverter@noreply.github.com mailto:CodeConverter@noreply.github.com > Cc: Bob Frankston <Github@bob.ma mailto:Github@bob.ma >; Author <author@noreply.github.com mailto:author@noreply.github.com > Subject: Re: [icsharpcode/CodeConverter] VB -> C# LINQ Etc. (#316)

Next steps on this are to have a look at whether the equals operator in VB linq does the same as outside of linq, and whether we could detect a sql context for example and special case it.

— You are receiving this because you authored the thread. Reply to this email directly, https://github.com/icsharpcode/CodeConverter/issues/316?email_source=notifications&email_token=ABJ4ONCPJWMWLV5GYVSYZZDPYQZLXA5CNFSM4HP57NEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWX6CZY#issuecomment-498065767 view it on GitHub, or https://github.com/notifications/unsubscribe-auth/ABJ4OND6XAYP3GDGD6MK3JDPYQZLXANCNFSM4HP57NEA mute the thread. https://github.com/notifications/beacon/ABJ4ONCPFOSQ42ZG3NKTR3TPYQZLXA5CNFSM4HP57NEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWX6CZY.gif

1reaction
BobFrankstoncommented, May 30, 2019

My use case is doing my own projects which have evolved over 20 years. The async was added in a shift from using threads and when it worked well-enough I moved on. I will tell when I do figure it out but more likely will just cleanup the code overall so may not know the key thing I could’ve gotten away with.

What would help most is the ability to do incremental changes and go back without losing the tweaks I had to make. This is why I like the ideas of pragmas for LINQ – I can capture the knowledge rather than having to repeat the process each iteration.

Making a copy of the whole directory and naming back the CSPROJ=>VBPROJ works adequately for me. I presume production shops have better procedures so no need to do too much to accommodate me.

Another general issue is reference VB modules from other VB code after it changes to C#. For now I put that project aside because of the need to change other project. Not sure how to write C# code that is fully transparent. (going the other way “using static” is easier).

In any case, thanks again for all your effort.

Bob Frankston

https://Frankston.com https://Frankston.com

From: GrahamTheCoder notifications@github.com Sent: Wednesday, May 29, 2019 18:17 To: icsharpcode/CodeConverter CodeConverter@noreply.github.com Cc: Bob Frankston Github@bob.ma; Author author@noreply.github.com Subject: Re: [icsharpcode/CodeConverter] VB -> C# LINQ Etc. (#316)

Thanks. The case of something that works due to lenience in VB, but not in c# is the case I’m interested in. E.g. Does VB add an await automatically somewhere. Does await in vb do a null check behind the scenes that should be replicated when converted etc.

I generally agree an undo function would be useful, mainly as a move towards a more step by step guide to conversion within the tool. For now I recommend using a version control system (such as git) to provide such functionality. However, I have attempted to provide a manual route, in the form of .bak files for files that are changed in the process (in addition to a confirmation dialog). A very simple script could automate restoring the bak files if there are a lot. Just to check, is there a specific shortcoming you’re finding with one of those approaches that doesn’t suit your use cases? (Even their discoverability)

Thanks, Graham.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/icsharpcode/CodeConverter/issues/316?email_source=notifications&email_token=ABJ4ONAJZ4YAMSN4GB33OBTPX36EFA5CNFSM4HP57NEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWQZK6Y#issuecomment-497128827 , or mute the thread https://github.com/notifications/unsubscribe-auth/ABJ4ONEZNJ7GMPQJSNFJDX3PX36EFANCNFSM4HP57NEA . https://github.com/notifications/beacon/ABJ4ONGFMFOY7PNX7KEJUS3PX36EFA5CNFSM4HP57NEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWQZK6Y.gif

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to LINQ Queries (C#)
In a LINQ query, you are always working with objects. ... A queryable type requires no modification or special treatment to serve as...
Read more >
Expression(Of Func(Of T)).Body.Member.Name bizarre "$ ...
The called Method receives that Lambda expression in a System.Linq.Expressions.Expression<Func<T>> type in C# or Expression(Of Func(Of T)) in VB ...
Read more >
Chapter 3. LINQ building blocks - LINQ in Action
An introduction to the key elements of the LINQ foundation; Sequences; Deferred query execution; Query operators; Query expressions; Expression trees; ...
Read more >
LINQ Expression
LINQ introduced the new type called Expression that represents strongly typed lambda expression. It means lambda expression can also be assigned to ...
Read more >
LINQ to SQL (Part 9 - Using a Custom LINQ Expression with ...
Once you retrieve a sequence of data, all you need to-do is to assign it to the "Result" property on the LinqDataSourceSelectEventArgs object....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found