5

I had a list List<string> Rank

items in list Rank are

"FF"
"ABC"
"CC"
"FF"
"FF"

I want a linq query that return value if exist and its count, suppose If i search for "FF" then it should return

value: ff
count: 3

currently i am using this query to find the match

var match = Rank.FirstOrDefault(s => s.Contains("FF"));

and this query to group the same values and assign them count.

var f = Rank.GroupBy(x => x).Select(g => new { Value = g.Key, Count = g.Count() });

i tried this but it return me complete list, it look like where clause not

var f = Rank.GroupBy(x => x).Select(g => new { Value = g.Key, Count = g.Count() }).Where(s => Rank.Contains("FF"));

can anybody know why third query is not working?

1
  • Try using the count() method. It allows you to provide a match expression similar to Where. Commented Nov 12, 2013 at 7:28

3 Answers 3

6

This is nearly correct

var f = Rank.GroupBy(x => x)
            .Select(g => new { Value = g.Key, Count = g.Count() })
            .Where(s => Rank.Contains("FF"));

just change the end to so that you query newly created anonymous objects

var f = Rank.GroupBy(x => x)
            .Select(g => new { Value = g.Key, Count = g.Count() })
            .Where(s => s.Value == "FF");
Sign up to request clarification or add additional context in comments.

4 Comments

i would put the where clause before the select statement just to avoid the creation of not needed anonymous objects, but in that case the where clause should be .Where(g => g.Key.Contains("FF")). Also be aware that there is a big difference between checking for substring (like given in the question code) and checking for equality (like in this answer). So @Vijay should know all the incoming data and know which one works better.
+1 Good point! My answer just answers his question "why third query is not working".
Thanks Oliver for your suggestions. and @Ondrej: i marked your answer because it was really answer of my question but as functionality wise i found Wudzik ans. is more useful so i used his answer in my application.
@VijayRana Wudzik's code is far better solution, less error-prone when constructing complicated linq queries. As I said, I just answered your questinon so that you know the reason, not just better solution. Turning back on problem does not solve them :)
4

Try this

var matchCount = Rank.Count(x=>x.Contains("FF"));

it will return count of strings containing "FF" in your list, if you wan't to know if there is any item that matches your predicate, do:

if(Rank.Any(x=>x.Contains("FF"))
{
   ...
}

Comments

0
var list = new List<string>
               {
                   "FF", "ABC", "CC", "FF", "FF"
               };
string search = "FF";

var result = list.GroupBy(l => l).Where(g => g.Key.Contains(search)).Select(s => new Tuple<string, int>(s.Key, s.Count())).FirstOrDefault();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.