Linq count?

shers

Well-known member
Joined
Aug 12, 2007
Messages
86
Programming Experience
1-3
Hi,

I have a List of String from which I get all values and the count of each value. This is stored into an IEnumerable variable. So I get a (Value, Count) pair in the IEnumerable. I wish to get only the count from the Value, Count pair depending on the Value. Can I get this in one line of code using LINQ? I did manage to get the value. But how do I
display this in the console? Here is my code.

VB.NET:
 Dim i As IEnumerable = attRefs.GroupBy(Function(x) x).[Select](Function(g) New With { _
                                Key .Value = g.Key, _
                                    Key .Count = g.Count() _
                                    }).OrderByDescending(Function(x) x.Count)

 Dim at = (From it In i Where it.Value = "12345" Select it.count)

Thanks
 
Dim count = (From value In attRefs Where value = "12345").Count

or
Dim count = attRefs.Select(Function(value) value = "12345").Count()
 
Dim count = (From value In attRefs Where value = "12345").Count

or
Dim count = attRefs.Select(Function(value) value = "12345").Count()
That second one should be Where rather than Select, but there's no need to call Where and Count because Count can accept a predicate itself:
Dim count = attRefs.Count(Function(value) value = "12345")
 
Ah yes, now that you mention that, I have encountered that issue before. I think I may have used LongCount in that situation instead, although that's not an option if you need an Integer. You could call Cast on the List first, actually cast the List as an IEnumerable or, as you did, call Where separately.
 
Back
Top