Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:23:53+00:00 2026-06-08T00:23:53+00:00

I have a query in linq but i am unable to understand it, because

  • 0

I have a query in linq but i am unable to understand it, because i didn’t write this query. The query is below

string[] arr1 = new string[]
                        {
                            "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
                            "Pakistan:4,India:3,USA:2,Iran:1,UK:0",
                            "India:4,USA:3,Iran:2,UK:1,Pakistan:0"
                        };

        var count = arr1
            .SelectMany(s => s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            .GroupBy(s => s.Split(':')[0], s => s.Split(':')[1])
            .ToDictionary(g => g.Key,
                 g =>
                 {
                     var items = g.Distinct();
                     var result = new Dictionary<String, int>();
                     foreach (var item in items)
                         result[item] = g.Count(gitem => gitem == item);
                     return result;
                 });


        // print the result
        foreach (var country in count.Keys)
        {
            foreach (var ocurrence in count[country].Keys)
            {
                Console.WriteLine("{0} : {1} = {2}", country, ocurrence, count[country][ocurrence]);
            }
        }

Purpose of this query

this query is performed to achieve this: We want to know that how many times Pakistan comes with ‘0’ , how many times with ‘1’ ,2 ,3 , 4 , and we want to get this information for all countries . Please can anyone define it step by step .Thanks

Note : this query is compiled , has no errors and is working properly

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:23:54+00:00Added an answer on June 8, 2026 at 12:23 am

    Let’s look at the query, step by step

    SelectMany takes the result of splitting on ‘,’ and combines the various splits back into a single IEnumerable

    Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

    So at that stage, you have converted the array arr1 into a single IEnumerable<string> with values like like USA:2

    GroupBy then groups that IEnumberable<string>. It splits each entry like USA:2 using the ‘:’ character. The left-hand side is the grouping key and the right-hand side is the value.

    Groups the elements of a sequence according to a specified key selector function.

    ToDictionary transforms that grouping into a Dictionary. The key of the Dictionary entry is the grouping key (e.g. USA). The value of the Dictionary entry is a new (anonymous) object:

    g =>
                 {
                     var items = g.Distinct();
                     var result = new Dictionary<String, int>();
                     foreach (var item in items)
                         result[item] = g.Count(gitem => gitem == item);
                     return result;
                 }
    

    The anonymous object is constructed by going through and counting the unique occurrences of a given count (e.g. if the key is USA, how many times is the value 1) and returns that count.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am unable to solve this problem with the LINQ Query. So we have
I have just started Entity Framework & linq and write this query var query
I have a LINQ to ENTITY query that pulls from a table, but I
I have a Linq query that looks something like this: var myPosse = from
I have this linq query that works well (although it may be written better,
I have a Linq query that looks something like this: var query = from
I have a LINQ query that returns some object like this... var query =
I have this query that gets executed though Linq to Entities. First time the
I have a LINQ query that has the incorrect results, but when I profile
i have this query (linq to entity) : var query = from p in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.