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 8786103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:31:53+00:00 2026-06-13T21:31:53+00:00

I have two lists which I am getting from database as follow: List<myobject1> frstList

  • 0

I have two lists which I am getting from database as follow:

List<myobject1> frstList = ClientManager.Get_FirstList( PostCode.Text, PhoneNumber.Text);
                List<myobject2> secondList = new List<myobject2>;

                foreach (var c in frstList )
                {
                    secondList.Add( ClaimManager.GetSecondList(c.ID));
                }

now my list will contain data like so:

frstList: id = 1, id = 2
secondList: id=1 parentid = 1, id=2 parentid=1 and id = 3 parentid = 2

I want to count these individually and return the one that has most counts? in above example it should return id=1 from frsList and id1 and id2 from secondList…

tried this but not working

var numbers = (from c in frstList where c.Parent.ID == secondList.Select(cl=> cl.ID) select c).Count();

can someone please help me either in linq or normal foreach to do this?

Thanks

  • 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-13T21:31:54+00:00Added an answer on June 13, 2026 at 9:31 pm

    Looking at the question it appears that what you want is to determine which of the parent nodes has the most children, and you want the output to be that parent node along with all of its child nodes.

    The query is fairly straightforward:

    var largestGroup = secondList.GroupBy(item => item.ParentID)
      .MaxBy(group => group.Count());  
    
    var mostFrequentParent = largestGroup.Key;
    var childrenOfMostFrequentParent = largestGroup.AsEnumerable();
    

    We’ll just need this helper function, MaxBy:

    public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source
        , Func<TSource, TKey> selector
        , IComparer<TKey> comparer = null)
    {
        if (comparer == null)
        {
            comparer = Comparer<TKey>.Default;
        }
        using (IEnumerator<TSource> iterator = source.GetEnumerator())
        {
            if (!iterator.MoveNext())
            {
                throw new ArgumentException("Source was empty");
            }
    
            TSource maxItem = iterator.Current;
            TKey maxValue = selector(maxItem);
    
            while (iterator.MoveNext())
            {
                TKey nextValue = selector(iterator.Current);
                if (comparer.Compare(nextValue, maxValue) > 0)
                {
                    maxValue = nextValue;
                    maxItem = iterator.Current;
                }
            }
            return maxItem;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list which i am getting from database.I have 6 elements in
I have a two properties which holds lists. Whenever any item in this list
I have two lists from which I have to get two values(MyCaption and MyValue).
I have two datatables, one which lists foldes, and another which list's files within
I have two lists. First one is a list of Tasks pulled from an
I have two lists which are guaranteed to be the same length. I want
Background I have two lists, the first is items which contains around 250 tuples,
I've got two long lists A and B which have the same length but
I have an ordered list which I have styled in two ways: Coloured the
I have two polygons which are defined by a list of points: x1,y1; x2,y2;

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.