0

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.

David Gidony
  • 1,243
  • 3
  • 16
  • 31

2 Answers2

2

You just need to use the overload of Count or Where ... Count:

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

or

int count = smartcardCollection.Where(s => s.AssignedUserName == my String).Count();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

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

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47