Converting VB LINQ to C# LINQ

VB.Net to C# Converter is one of the few conversion tools that knows about LINQ and lambdas and the other 3.5 stuff. I turned to a trial version of the product after going in circles on this VB code conversion:

Dim q = From c In dc.Categories, _
        p In dc.Products _
        Where c.CategoryID = p.CategoryID _
        Group p By c.CategoryName Into Group _
        Select New With _
        {.cgname = CategoryName, _
         .prdcts = Group}

While other tools choked, VBConversions churned out this:

var q = (from c in dc.Categories
         from p in dc.Products
         where c.CategoryID == p.CategoryID
         group p by c.CategoryName into g
         let CategoryName = g.Key
         let Group = g.ToArray()
         select new { CategoryName, Group }).Select(s =>
          new { cgname = s.CategoryName, prdcts = s.Group });

I'm not sure I would have figured that out on my own.

Actually, I fixed a small bug in the conversion. The original result had the assignment operator (=) instead of the comparison operator (==) in the where clause. When I reported this issue to the author, I got a prompt, polite, and appreciative response with the assurance of a fix in the next release.

One thing that I'd like to see is a snippet converter in the tool. Right now, you need to point the converter to a VS project file - something I don't normally use in file-based, ASP.NET 3.5 Web development.

For more info on VB.Net to C# Converter, see http://vbconversions.net/ .

BTW, I had to smile at C#'s use of "let" in LINQ syntax. Don't I remember Let from the earliest BASIC days?

4 Comments

  • As for me I really go c#, when I need to convert an algorithm I just do it by Hand, may be if it's about linq, I may rewrite it or use the Visual Linq Builder

  • Ken, thanks for pointing out the equal sign problem in the LINQ Where clause conversions.

    It turns out that this bug only happens when there's a "Select New With" clause in the LINQ statement. The converter thought the Where clause equal sign was one of the equal signs in the With clause (which should be converted to "=").

    This has been fixed in the latest version of the VB.Net to C# Converter, which was just released today. If you find any more problems, please let me know and I'll get a fix out asap. Thanks!

  • Kudos to Dave at VB Conversions (our honorable competitor) for getting the jump on us regarding many types of LINQ conversions, but a simpler conversion is just:

    var q = from c in dc.categories
    from p in dc.products
    where c.CategoryID == p.CategoryID
    group p by c.CategoryName into g
    select new {cgname = g.Key, prdcts = g};

  • I tried using the program to convert the following LINQ query to C#:

    Dim qry = From d In _rawData _
    Order By d.FormatName, d.QualityName _
    Group d By d.FormatName, d.QualityName _
    Into MyAvg = Average(d.GrossWeeklyPanelRate), MyCount = Count(d.GrossWeeklyPanelRate), _
    MyMin = Min(d.GrossWeeklyPanelRate), MyMax = Max(d.GrossWeeklyPanelRate)

    Which resulted in:

    var qry = from d in _rawData

    orderby d.Format, d.Quality
    group d by new
    {
    d.Format,
    d.Quality
    }
    into g
    let FormatName = g.Key.Format
    let QualityName = g.Key.Quality
    let MyAvg = g.Average(d.GrossWeeklyPanelRate)
    let MyCount = g.Count(d.GrossWeeklyPanelRate)
    let MyMin = g.Min((d.GrossWeeklyPanelRate)
    let MyMax = g.Max(d.GrossWeeklyPanelRate)
    select new { FormatName, QualityName, MyAvg, MyCount, MyMin, MyMax };

    However, I get an error in the aggregate function: The name 'd' does not exist in the current context. How am I to get the average, count, minimum and maximum of a field with my data?

Comments have been disabled for this content.