Search code examples
c#listlinqcountreadonly-collection

c# - How to count the number of occurences of a string in a ReadOnlyCollection field


I have a ReadOnlyCollection of a Smartcard type from Microsoft.Clm.Shared.Smartcards namespace.

one of the fields/parameters of the smartcard object is AssingnedUserName.

I need to be able to count how many times a smartcard with the same username exist in the list,

something like:

[Pseudo Code]
int count = (smartcardCollection.AssignedUserName == my String).Count().

I tried to use the ReadOnlyCollection.Tolist() method, but I couldn't find the correct syntax to make it work.

I also found many examples but non for a ReadOnlyCollection object !

what is the best practice for achieving this ?

thanks

David.


Solution

  • just use this

    int count = smartcardCollection.Count(s=>s.AssignedUserName == my String); 
    

    LINQ Count it takes a function to test each element for a condition