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

linq, vb.net

Solution

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()

Problem

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() ```

Original source