Adding a SQL WHERE clause into linQ - vb.net

Using VS 2013 (VB).
I have the following linq query
Dim countCompanies = (From num In db.GasEmailAddresses
Group Join GCC In db.GasCompanies
On num.CompanyID Equals GCC.ID
Into GCComp = Group
Select num.CompanyID).Distinct.Count
Which returns 4 results. I want to add a where clause along the lines of
WHERE GCC.Active = 1
I have been messing with this bit of code for a bit now and cant seem to get it to work.
Any ideas?
---UPDATE---
Revised code based on suggestion
Dim countCompanies = (From num In db.GasEmailAddresses
Group Join GCC In db.GasCompanies.Where(Function(y) y.Active = 1)
On num.CompanyID Equals GCC.ID
Into GCComp = Group
Select num.CompanyID).Distinct.Count

Try filtering out while retrieving the data source:-
companies.Where(Function(x) x.Active = 1)

Related

Can't figure out how to add current date to comment

I'm having problems with my sql code, i'm trying to implement a current date to my comment but i can't figure out how + i'm having syntax errors and i don't know what to do anymore. Can someone help me with the date adding to comment?
UPDATE Osalus_projektis AS op
SET op.töötasu = op.töötasu + 100,
op.comment = CONCAT(NOW()'tõusis palk 100 eurot')
FROM Osalus_projektis
INNER JOIN Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
WHERE ol.nimetus = 'nõustaja';
MS Access uses & for string concatenation. And it doesn't support a FROM clause. This may do what you want:
UPDATE Osalus_projektis AS op INNER JOIN
Osaluse_liik AS ol
ON ol.osaluse_liik = op.osaluse_liik
SET op.töötasu = op.töötasu + 100,
op.comment = NOW() & 'tõusis palk 100 eurot'
WHERE ol.nimetus = 'nõustaja';
If there is a problem with the JOIN -- which happens a lot in MS Access -- then you can use an EXISTS clause as well.

Linq, SQL, and ISNULL

I'm trying to figure Linq out. I'm having very little success and most of the articles are for C# which isn't helping.
I'm trying to make the following work;
Dim query = From r In db.eq_list
Join s In db.interview_main On r.CLIENTCODE Equals s.CLIENTCODE And r.CONTROL Equals s.CONTROL
Select New With {r.UserName, r.CONTROL, r.CLIENTCODE, r.CLIENTLOCATION, r.IDATETIME, r.FIRSTNAME,
r.LASTNAME, If(String.IsNullOrEmpty(s.Code), 0, s.Code)}
dgvOnHold.DataSource = query.ToList
The problem is the IF part. This part can be NULL in the database, but I want it returned as 0 if NULL. I'm putting this into a read-only Datagridview. The original SQL is as follows;
SELECT r.ID,r.UserName,r.CONTROL,r.CLIENTCODE,r.CLIENTLOCATION,r.IDATETIME,r.FIRSTNAME,r.LASTNAME,ISNULL(s.CODE,0) AS CODE
FROM system.eq_list AS r
LEFT JOIN interview.main AS s ON r.CLIENTCODE = s.CLIENTCODE AND r.CONTROL = s.CONTROL;
Can't check this at the moment but how about?
Dim query = From r In db.eq_list
Join s In db.interview_main On r.CLIENTCODE Equals s.CLIENTCODE And r.CONTROL Equals s.CONTROL
Select New With {r.UserName, r.CONTROL, r.CLIENTCODE, r.CLIENTLOCATION, r.IDATETIME, r.FIRSTNAME,
r.LASTNAME, If(s.Code is Nothing, 0, s.Code)}
dgvOnHold.DataSource = query.ToList

Distinct ComboBox Items

I am trying to convert sql to entity and I need to select distinct items. I thought this would work but its returning all the rows instead of the distinct items.
Dim OrderNos = (From r In Orders.R3Delivery Where r.mainOrderNumber <> "" Select r).Distinct().ToList()
For Each thisentry In OrderNos
cbOrderNumbers.DisplayMember = thisentry.mainOrderNumber
cbOrderNumbers.ValueMember = thisentry.mainOrderNumber
Next
Also is their any good free sql to linq tools out their linquer good but its like 60 quid
The problem is that the Distinct() is comparing the entire object being returned, not just the order number.
If you only need the order numbers, changing this line should get you there:
Dim OrderNos = (From r
In Orders.R3Delivery
Where r.mainOrderNumber <> ""
Select r.mainOrderNumber).Distinct().ToList()
If you need the whole object, then it gets more complicated.

Sum from LINQ query

Cannot get this to work. Want to get the sum of all the amounts int the database from the following LINQ statement:
Dim currentPaid = From a In db.Payments
Where a.ForJob = id
Select a.Amount
Dim totalPaid As Double = currentPaid.Sum()
As it underlines the second statment and says:
Overload resolution failed because no accessible 'Sum' accepts this number of attributes.
Asked another question with code and someone answered my question exactly so the answer is below:
Dim totalPaid = db.Payments.Where(Function(a) a.ForJob = jobId).Sum(Function(a) a.Amount)
Dim sum = (From a In db.Payments Where a.ForJob = id Select a.Amount).Sum();

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 )