Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select count on list using linq on specific value vb.net

Tags:

vb.net

linq

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()
like image 641
John Janssen Avatar asked Jan 20 '26 01:01

John Janssen


1 Answers

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()
like image 169
Tim Schmelter Avatar answered Jan 22 '26 17:01

Tim Schmelter