linq sum in vb.net - 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.

Related

LINQ - left join to find unmatched records

I'm trying to execute a left join between 2 datatables that will return all records from the left table without a corresponding value in the right table on the join criteria. As of now I have the following which returns nothing:
Dim Query1 = From exasset In dtExistingAssets _
GroupJoin asset In dtNewAssets _
On exasset("ACCOUNT_NAME") Equals asset("ACCOUNT_NAME") _
Into results = Group _
From f In results.DefaultIfEmpty _
Where IsDBNull(f) _
SelectNewWith _
{ //...
I've seen several references to using Any but I wasn't able get the syntax correct. Can anyone please help out? This is something that is really simple to accomplish in SQL but seems a lot more complicated in LINQ.
I think the problem is the IsDBNull(f), a left join will result in a null value (Nothing in VB) not a DBNull value. I think you should change it to: ``
...
From f In results.DefaultIfEmpty _
Where f is Nothing
I would use the strongly typed DataRow extension methods like Field which also support nullables.
Dim query = From exAsset In dtExistingAssets
Group Join newAsset In dtNewAssets
On exAsset.Field(Of String)("ACCOUNT_NAME") Equals newAsset.Field(Of String)("ACCOUNT_NAME") Into Group
From joinedAssets In Group.DefaultIfEmpty()
Where joinedAssets.Field(Of String)("ACCOUNT_NAME") Is Nothing
If you just want to know the new accounts, you can also use the efficient Enumerable.Except:
Dim existingAccounts = From exRow In dtExistingAssets
Select exRow.Field(Of String)("ACCOUNT_NAME")
Dim newAccounts = From newRow In dtNewAssets
Select newRow.Field(Of String)("ACCOUNT_NAME")
Dim newAccNotInExisting = newAccounts.Except( existingAccounts )

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.

Pivot Table in LINQ in 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.

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.