Domanda

I am trying to find a well structured example to create a Pivot table using linq to SQL in VB.net. there are many examples of how to do this in C# but only 2 in VB.net that did not solve my problem.

I have tried to use the c# to VB converters to change the code to VB.net but these do not appear to work with LINQ.

I also tried to convert an SQL Pivot table to LINQ for VB.net using Linqer, Linquer does not support the MS SQL command Pivot.

an example of the data is,

name    prod    status
a1      1       1
a1      2       2
a2      3       1

result should be

name    prod1    prod2    prod3
a1      1        2        0
a2      0        0        1

First objective would be to write a linq query to return the pivot table, the desired result is to perform a distinct query on the "prod" column and create a dynamic pivot table.

Having looked at all the C# examples i can see that if i want to use linq i will need to move code to C#, unfortunately I only know VB.net.

Any examples that use VB.net with the function option would be useful

this example written in c# seems to address my request, unfortunately I cannot convert it to VB.net.

 var query = from foo in db.Foos
        group foo by foo.Date into g
        select new {
            Date = g.Key,
            A = g.Where(x => x.Employee == "A").Sum(x => x.Job1),
            B = g.Where(x => x.Employee == "B").Sum(x => x.Job1),
            C = g.Where(x => x.Employee == "C").Sum(x => x.Job1),
            D = g.Where(x => x.Employee == "D").Sum(x => x.Job1),
            Total = g.Sum(x => x.Job1)
        };
È stato utile?

Soluzione

You can try something like this. (NOT TESTED) Just used your c# LINQ to convert to VB

Dim test = customers.GroupBy(Function(xCustomer) xCustomer.CustId) _
               .Select(Function(xCustomers) 
                           New With {xCustomers.Key,
                           .Jan = xCustomers.Where(Function(y) y.OrderDate.Month = 1).Sum(Function(s) s.Qty),
                           .Feb = xCustomers.Where(Function(y) y.OrderDate.Month = 2).Sum(Function(s) s.Qty)})

EDIT I have added Feb in the code. hope it helps. You can just go on adding such properties in the anonymous type.

EDIT

I used a simple VB Console app with the data that you provided in your post, and it works absolutely fine. You can try this out.

Sub Main()

    Dim samples As New List(Of Sample)
    samples.Add(New Sample() With {.Name = "a1", .Prod = 1, .Status = 1})
    samples.Add(New Sample() With {.Name = "a1", .Prod = 2, .Status = 2})
    samples.Add(New Sample() With {.Name = "a2", .Prod = 3, .Status = 1})

    Dim test = samples.GroupBy(Function(xSample) xSample.Name) _
               .Select(Function(xGrouping) New With { _
                           xGrouping.Key,
                           .Prod1 = xGrouping.FirstOrDefault(Function(x) x.Prod = 1), _
                           .Prod2 = xGrouping.FirstOrDefault(Function(x) x.Prod = 2), _
                           .Prod3 = xGrouping.FirstOrDefault(Function(x) x.Prod = 3) _
                           })

    Console.Write("Name")
    Console.Write(Microsoft.VisualBasic.vbTab)
    Console.Write("Prod1")
    Console.Write(Microsoft.VisualBasic.vbTab)
    Console.Write("Prod2")
    Console.Write(Microsoft.VisualBasic.vbTab)
    Console.Write("Prod3")
    Console.Write(Microsoft.VisualBasic.vbTab)
    Console.WriteLine()
    For Each test1 In test
        Console.Write(test1.Key)
        Console.Write(Microsoft.VisualBasic.vbTab)
        If (test1.Prod1 IsNot Nothing) Then
            Console.Write(test1.Prod1.Status)
        Else
            Console.Write(0)
        End If
        Console.Write(Microsoft.VisualBasic.vbTab)
        If (test1.Prod2 IsNot Nothing) Then
            Console.Write(test1.Prod2.Status)
        Else
            Console.Write(0)
        End If
        Console.Write(Microsoft.VisualBasic.vbTab)
        If (test1.Prod3 IsNot Nothing) Then
            Console.Write(test1.Prod3.Status)
        Else
            Console.Write(0)
        End If
        Console.WriteLine()
    Next
    Console.ReadLine()
End Sub
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top