Pivot Table in LINQ in VB.NET - vb.net

I'm looking for doing a Pivot Table query in vb.net.
I've found a lot of code in c# but not in VB.Net and i don't understand how to convert c# to VB.
Can you help me?
Thanks
Ju

I found the response:
Dim query = From final In _
(From lh In Me.AnnualCost1.sp_annualcostLH _
Group lh By lh.Asset_Number Into Group) _
Let Jan As Object = (From r In final.Group Where _
r.Month = "January" _
Select r.lhTotal).Sum _
Select New With { _
.Asset_Number = final.Asset_Number, _
.January = If(Jan Is Nothing, 0, Jan) _
}
Thats generate a pivot Table with column by month. Here is only for january
Julien

You can use SharpDevelop to convert C# code to VB.net and reverse and also to few other languages.

Related

linq query from one to many to many not letting me retrieve last many

I have a jobmain table which has a single jobnumber for every job, then a jobparts table which has many jobparts for each job, then a jobpartsforms which has many forms for every job part. Linq only allows the following:
Dim query = From jst In db.JobMains _
Where jst.JobNum = CInt(Session("JobNumber")) _
Select jst.JobParts
but I want to get the jobpartsforms which should look like this:
Dim query = From jst In db.JobMains _
Where jst.JobNum = CInt(Session("JobNumber")) _
Select jst.JobParts.JobPartsForms
How can I do this and why doesn't it work the way I think it should since the relationships are all built in already?
You need to flatten the query before selecting the JobPartsForms.
Dim query = From jst In db.JobMains _
Where jst.JobNum = CInt(Session("JobNumber")) _
From part in jst.JobParts _
Select part.JobPartsForms
Or with lambda syntax you can use SelectMany:
db.JobMains _
.Where(Funtion(j) j.JobNum = CInt(Session("JobNumber")) _
.SelectMany(Funtion(j) j.JobParts) _
.Select(Function(jp) jp.Forms)

Datatable Grouping Using Linq and VB .NET

I'm completely dense here, but I'm trying to get some stats from a DataTable. One of the columns in the datatable is called "colour".
I need to find out how many of each instance of "colour" are in the datatable.
I'm trying:
Dim q = From p In PGWorkingDataTable _
Group p By p("colour") Into Group _
Select Group
But I get the compiler error at design-time:
"Range variable name can be inferred only from a simple or qualified name with no arguments" on the p("colour") section.
I need some serious guidance here. Thanks for your help.
Joe
You need to specify a name for the group key:
From p In new DataTable() _
Group p By Color = p("colour") Into Group _
Select Group
Writing LINQ in VB.NET is nobody's favorite thing. Try something like the following:
Dim q = From p In PGWorkingDataTable _
Group By colour = p("colour") _
Into colourCount = Count(p("colour")) _
Select colour, colourCount

Select two datacolumns from a datarow in linq (vb.net 3.5)

Dim orders = From tt In testTable _
Order By tt.Item("OrderNumber") _
Select tt.Item("OrderNumber"), tt.Item("OrderId")
This is breaking. Is there a way to do this? I would have thought it was easy enough. Obviously, I thought wrong....
Dim orders = From tt In testTable _
Order By tt.Item("OrderNumber") _
Select New With {.OrderNo = tt.Item("OrderNumber"), .OrderId = tt.Item("OrderId")}
If I got the VB.NET syntax right
This returns an anonymous type, if you want to return an existing type you replace With with that type.

linq sum in vb.net

I need to make a query that look like this is SQL:
SELECT CodProiect, SUM(Valoare)
FROM DET
WHERE CodProiect = 'cod_pr'
GROUP BY CodProiect;
How can I write the same thing in LINQ?
I have attempted this:
dim gac1 = (From ac1 In t_detalii _
Where ac1!CodProiect = cod_pr1 _
Select ac1!Valoare).Sum(Function(ac1) ac1!Valoare)
But it gives me an error "No default member found for type 'Double'."
You need to select ac1 instead of ac1!Valoare (which is the property itself, not the corresponding object)
dim gac1 = (From ac1 In t_detalii _
Where ac1!CodProiect = cod_pr1 _
Select ac1).Sum(Function(ac1) ac1!Valoare)
edit
You can of course group it in the query already:
dim gac1 = (From ac1 In t_detalii _
Where ac1!CodProiect = cod_pr1 _
Group By ac1.CodProiect Into ac2
Select ac2.Sum(Function(ac2) ac2!Valoare)
(something in that direction, I'm not familiar with the vb.net syntax, also I'm writing it without compilersupport atm.

Linq to XML. Selecting Multiple columns

Dim MyQuery = From c In xdoc.Descendants() _
Where c.Attribute(OriginY) IsNot Nothing _
Order By Val(c.Attribute(OriginY).Value), Val(c.Attribute(OriginX).Value) _
Select c.Attribute(UniStr)
Right above you can see my First! linq attempt! And here comes my first question.
How can i select more than one column in a linq query in vb.net?
For example... Select c.Attribute(UniStr) AND c.Attribute(OriginY)
Not really a VB.NET person, but this should work (Providing your existing statement compiles):
Dim MyQuery = From c In xdoc.Descendants() _
Where c.Attribute(OriginY) IsNot Nothing _
Order By Val(c.Attribute(OriginY).Value), Val(c.Attribute(OriginX).Value) _
Select UniStr = c.Attribute(UniStr), OriginY = c.Attribute(OriginY)
You should read about Projections in VB.NET and LINQ.