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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:47:29+00:00 2026-06-18T05:47:29+00:00

I have a Generic list say List<TransactionInfo> TransactionInfo class have three properties:- public long

  • 0

I have a Generic list say List<TransactionInfo>

TransactionInfo class have three properties:-

public long TransactionId { get; set; }
public string BuyerName { get; set; }  
public string SellerName { get; set; }

If below is data in my list:-

1 A B   
2 A C
3 A D
4 G H
5 C M
6 D E
7 A F
8 H L
9 L R
10 Y Z

Then I want to create group clusters based on the transactions between members. I want to write LINQ query that should return new List

ClusterInfo Class contains one property:-

public List<TransactionInfo> Transactions { get; set; }

List<ClusterInfo> should contain:-

clusters[0]:-

1 A B
2 A C
3 A D
5 C M
6 D E
7 A F

clusters[1]

4 G H
8 H L
9 L R

clusters[2]

10 Y Z

How these new lists are selected:-
All the direct or indirect Buyers/Sellers should be part of one cluster.

Can anyone help me in writing LINQ query to achieve this. If not LINQ then simple for loop solution will also suffice.

PS :- posting this from mobile.. so plz excuse bad formatting

  • 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-18T05:47:31+00:00Added an answer on June 18, 2026 at 5:47 am

    Here is the solution:

    static public void Main()
    {
        // Example of dataset
        List<TransactionInfo> transactionInfos = new List<TransactionInfo>()
        {
            new TransactionInfo(1, "A", "B"),
            new TransactionInfo(2, "A", "C"),
            new TransactionInfo(3, "A", "D"),
            new TransactionInfo(4, "G", "H"),
            new TransactionInfo(5, "C", "M"),
            new TransactionInfo(6, "D", "E"),
            new TransactionInfo(7, "A", "F"),
            new TransactionInfo(8, "H", "L"),
            new TransactionInfo(9, "L", "R"),
            new TransactionInfo(10, "Y", "Z"),
        };
    
        // Creates the graph and add the arcs
        UndirectedGraph<string> graph = new UndirectedGraph<string>();
        foreach (TransactionInfo transactionInfo in transactionInfos)
            graph.AddArc(transactionInfo.SellerName, transactionInfo.BuyerName);
    
        var result = (from clusterByNode in graph.GetConnectedComponents()  // Gets the connected components
                      select (from transactionInfo in transactionInfos  // Gets the transaction infos
                              join node in clusterByNode on transactionInfo.BuyerName equals node  // Joins the transactionInfo with the node in the connected component
                              select transactionInfo).ToList()).ToList();
    }
    

    With the class:

    public class TransactionInfo
    {
        public long TransactionId { get; set; }
        public string BuyerName { get; set; }
        public string SellerName { get; set; }
    
        public TransactionInfo(long transactionId, string buyerName, string sellerName)
        {
            TransactionId = transactionId;
            BuyerName = buyerName;
            SellerName = sellerName;
        }
    }
    

    And, the class that find the connected components:

    public class UndirectedGraph<T>
    {
        private Dictionary<T, HashSet<T>> linkedNodes = new Dictionary<T, HashSet<T>>();
    
        public void AddNode(T node)
        {
            if (!linkedNodes.ContainsKey(node))
                linkedNodes.Add(node, new HashSet<T>());
        }
    
        public void AddArc(T node1, T node2)
        {
            if (!linkedNodes.ContainsKey(node1))
                linkedNodes.Add(node1, new HashSet<T>());
            linkedNodes[node1].Add(node2);
    
            if (!linkedNodes.ContainsKey(node2))
                linkedNodes.Add(node2, new HashSet<T>());
            linkedNodes[node2].Add(node1);
        }
    
        public IEnumerable<HashSet<T>> GetConnectedComponents()
        {
            HashSet<T> visitedNodes = new HashSet<T>();
    
            foreach (T nodeToVisit in linkedNodes.Keys)
            {
                if (!visitedNodes.Contains(nodeToVisit))
                {
                    HashSet<T> connectedComponent = GetConnectedComponent(nodeToVisit);
                    visitedNodes.UnionWith(connectedComponent);
                    yield return connectedComponent;
                }
            }
        }
        private HashSet<T> GetConnectedComponent(T from)
        {
            HashSet<T> result = new HashSet<T>();
    
            Stack<T> nodesToVisit = new Stack<T>();
            nodesToVisit.Push(from);
            result.Add(from);
    
            while (nodesToVisit.Count != 0)
            {
                T nodeToVisit = nodesToVisit.Pop();
    
                foreach (T linkedNode in linkedNodes[nodeToVisit])
                {
                    if (!result.Contains(linkedNode))
                    {
                        nodesToVisit.Push(linkedNode);
                        result.Add(linkedNode);
                    }
                }
            }
    
            return result;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a generic list of the the following objects: public class
Let's say I have a generic Object class, and a generic List class. I
Let's say I have a generic class: public class MyGenericClass<T> { ... } Now
Say, we have a generic class with a private List. We can make it
Let's say you have a Generic Class which have a List<T> Items; Now think
I have a generic list of type Element , for example. public class Element
I have a Generic List as below public static readonly List<Customer> Customers = new
Lets say we have a generic list of Class1, typically having ~100 objects for
I have two generic list that have been unioned. Say listA and listB, both
Say I have a base class: TPart = class private FPartId: Integer; public property

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.