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();
Related
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
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)
I am having an issue getting the results I am looking for. What I need to do is essentially:
Select count(invoicenbr) from invoicelist where invoicenbr = 'invoice'
but when I try it in linq I am not getting the correct results.
When I try to execute the linq query below, it gives me the count for the entire list, and not for the where invoicenbr = 'invoice'...
Here is my linq query that is returning the count for the entire invoiceList:
Dim test = (From invoices In invoicelist _
Where e.Row.Cells("invoicenbr").Value = invoice).count()
You have a naming issue in your code, i assume that it's responsible for your problem.
The variable in your query is invoices but later you use invoice to compare it with the cell value, so you have a different variable in scope with name invoice.
This should work:
Dim invoicenbr As String = e.Row.Cells("invoicenbr").Value
Dim duplicates = From invoice In invoicelist
Where invoice = invoicenbr
Dim duplicateCount As Int32 = duplicates.Count()
I have a typedDataset (ds) and its datatable(dt). The datatable has the columns Date(ttmmyyy) and Worktime. I want to sum the Worktime by month. I already accomplished to sum the Worktime with
Dim sum = dt.AsEnumerable().Sum((Function(x) x.Worktime))
I need help with group this by month.
I created a table with your fields and the following query worked nice:
Dim query = From row In dt
Group row By VarDate= row.Field(Of DateTime)("Date") Into MonthGroup = Group
Select New With {
Key VarDate,
.Worktime = MonthGroup.Sum(Function(r) r.Field(Of Int32)("Worktime"))
}
Used variable VarDate because Date is reserved for it's own class.
Output:
Vardate: 10/15/2013 Worktime: 150
Vardate: 10/16/2013 Worktime: 180
EDIT:
To get the output just use:
For Each obj In query
Dim datet = obj.VarDate
Dim WorkH = obj.Worktime
Next
To avoid declarying types when using linq, insert the line: option infer on on the very first line of the vb.net document ( so you can use dim datet = obj.Vardate ) without declaring the type. It is like var in c#
I have this SQL:
SELECT count (1) FROM users AS total_drafts WHERE version_replace = #sid
SELECT count (1) AS unpublished_drafts FROM users WHERE version_replace = #sid AND moderated = 0
SELECT * FROM users WHERE id = #sid ORDER By ID DESC
Which appears to be correct. However I'm having difficulty extracting the fields from the results. In vb.net I am using this code fragment:
While r.Read()
Dim o_email As String = CStr(r("email"))
Dim o_first_name As String = CStr(r("first_name"))
Dim o_last_name As String = CStr(r("last_name"))
Which is causing this error: System.IndexOutOfRangeException: email
I have checked the sql is being exucuted correctly. The sql I've posted is simply replacing a simpler statement which was feeding into the code fragment perfectly.
Why is this and how do I correct it?
the correct way:
While r.Read()
total_drafts = CInt(r("total_drafts"))
End While
r.NextResult()
While r.Read()
unpublished_drafts = CInt(r("unpublished_drafts"))
End While
error_status.Text = total_drafts & " " & unpublished_drafts
r.NextResult()
While r.Read()
Dim o_email As String = CStr(r("email"))
Dim o_first_name As String = CStr(r("first_name"))
Dim o_last_name As String = CStr(r("last_name"))
EDIT: r.NextResult() instead of r.ReadNext(), r.ReadNext() is for a DataTableReader
Assuming you are calling the whole sql statement in one go, the problem is that r.Read() will use the first datatable that is returned for the first statement(SELECT count (1) FROM users AS total_drafts WHERE version_replace = #sid ), which does not contain the email etc. fields.
You have to call r.NextResult() twice, this will move the datareader to the 3rd dataset that will contain the data from SELECT * FROM users WHERE id = #sid ORDER By ID DESC
You're returning three datasets. If "r" is a DataReader (unclear from your question) then you need to call;
r.NextResult
between your lines of code, like this;
While r.Read()
Dim o_email As String = CStr(r("email"))
r.NextResult()
Dim o_first_name As String = CStr(r("first_name"))
r.NextResult()
Dim o_last_name As String = CStr(r("last_name"))
One other possible explanation (again, unclear) is that you messed up your first column name ("email"), this would also give an out of range exception.
As far as I can understand you're trying to execute multiple statements, right?
You should separate your SQL statements with a semicolon and change the reader when you've finished with the previous one.
UPDATE:
I usually use stored procedures and return parameters for counters.
Something like this:
CREATE PROCEDURE usp_GetUsers (#sid INT, #unpublished INT OUTPUT)
AS
BEGIN
DECLARE #total_drafts INT
DECLARE #unpublished_drafts INT;
SELECT #total_drafts = count (1) FROM users WHERE version_replace = #sid
SELECT #unpublished_drafts = count (1) FROM users WHERE version_replace = #sid AND moderated = 0
SELECT * FROM users WHERE id = #sid ORDER By ID DESC
RETURN(#total_drafts)
END