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
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:
We’ll just need this helper function,
MaxBy: