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

  • Home
  • SEARCH
  • 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 9035605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:47:52+00:00 2026-06-16T08:47:52+00:00

I have a Dictionary<Key, <Quality,Item>> which is tracking the relationship between qualities and items.

  • 0

I have a Dictionary<Key, <Quality,Item>> which is tracking the relationship between qualities and items.
Qualities are an object type, Items are an object type, elsewhere I have lists of valid Qualities and valid Items. Items have a fixed list of qualities, always more than one. Qualities can be held by any number of items, including 0, depending on the state of the program.

Currently, Item objects also track their own Qualities in a List as one of my failed strategies to solve this. I have no idea if this is helpful or not, it’s sure not helping me right now and will probably be ripped out if proved useless.

I already have a LINQ self-join that collects pairs of unique Items that share at least one Quality successfully.

var r = from KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_1
        in QualitiesToItems
        join KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_2
        in QualitiesToItems
        on virtQ2I_1.Value.Item1.name equals virtQ2I_2.Value.Item1.name
        where (virtQ2I_1.Value.Item2.name != virtQ2I_2.Value.Item2.name)
        select new List<Item>
        {
            virtQ2I_1.Value.Item2, 
            virtQ2I_2.Value.Item2
        };

Afterwards I use another Dictionary to clean up the little burp where <ItemA, ItemB> are considered the same as <ItemB, ItemA>.

What Is Needed: A list of each triplet of unique Items that share at least one Quality with at least one other Item in the triplet. Big hairy complication: the third item in the triple can’t just share one of the existing shared Qualities; it must bring something new to the relationship.
And I need the results starting from a list of a few hundred Items quickly – my existing solution doesn’t meet this last requirement.

Example:

  • ItemA is Furry, Blonde, Four-Legged, and Trained
  • ItemB is Furry, Rowan, Six-Legged, and Trained
  • ItemC is Feathered, Blue, Two-Legged, and Trained
  • ItemD is Scaled, Rowan, Slithers, and Untrained

    • ItemA and ItemB will already have been picked up as a valid pair,
      sharing the qualities Furry and Trained. (B:D of course are another
      valid pair, as are A:C and B:C)

    • ItemA, ItemB, and ItemC do not make a valid triplet as A:B are
      already Trained, and ItemC has nothing else in common with either
      ItemA or ItemB; A:B:C has the same Qualities list as A:B and therefor
      C is rejected as “supernumerary” or “redundant”.

    • ItemA, ItemB, and ItemD make a valid triplet as ItemD forms a pair
      around Rowan with ItemB. The result of A:B:D is Furry, Rowan, Trained…
      and I need that combination of A:B:D to go in my list of returned results.

I’m having problems going from the way I’m getting my pairs to the way I need to get my triplets in a way that works on a few hundred items in reasonable time.

I thought I was very clever when I wrote a method to look for shared Qualities between two Items and used it in my new LINQ query, but the result is… quite slow when used on more than a score or so of items, and my computer is overpowered compared to some of the machines this will be running on.

var r = from KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_1 
        in QualitiesToItems 
        join KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_2 
        in QualitiesToItems
        on virtQ2I_1.Value.Item1.name equals virtQ2I_2.Value.Item1.name
        join KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_3
        in QualitiesToItems
        on virtQ2I_2.Value.Item1.name equals virtQ2I_3.Value.Item1.name
        where (virtQ2I_1.Value.Item2.name != virtQ2I_2.Value.Item2.name &&
        virtQ2I_1.Value.Item2.name != virtQ2I_3.Value.Item2.name &&
        virtQ2I_2.Value.Item2.name != virtQ2I_3.Value.Item2.name &&
        Item.SharedQualities(this, new Item[2] { virtQ2I_1.Value.Item2, virtQ2I_2.Value.Item2 }).Count !=
        Item.SharedQualities(this, new Item[3] { virtQ2I_1.Value.Item2, virtQ2I_2.Value.Item2, virtQ2I_3.Value.Item2 }).Count)
        select new List<Item>
        {
            virtQ2I_1.Value.Item2, 
            virtQ2I_2.Value.Item2, 
            virtQ2I_3.Value.Item2
        };

So: this worked, but I don’t like it. Is there a way to replace my function calls (and new item arrays) mid query with something pure LINQ? There must be.

  • 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-16T08:47:53+00:00Added an answer on June 16, 2026 at 8:47 am

    A little more head-bashing against the problem has provided a solution that does the worst of the heavy lifting in LINQ, and has much better performance than what I tried in the original post.

    //collect two-pair items
    var result = from KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_1
                        in QualitiesToItems
             join KeyValuePair<int, Tuple<Quality, Item>> virtQ2I_2
                     in QualitiesToItems
             on virtQ2I_1.Value.Item1.name equals virtQ2I_2.Value.Item1.name
             where (virtQ2I_1.Value.Item2.name != virtQ2I_2.Value.Item2.name)
             select new List<Item> {
                        virtQ2I_1.Value.Item2, 
                        virtQ2I_2.Value.Item2
                        };
    List<List<Item>> ItemsForSets = result.ToList();
    
    // self-join raw two-pair item list to generate three-set items
    result =    from List<Item> leftSide in ItemsForSets 
            join List<Item> rightSide in ItemsForSets
            on leftSide[1] equals rightSide[0]
            where (leftSide[0] != rightSide[1])
            select new List<Item> {
                        leftSide[0], 
                        leftSide[1],
                        rightSide[1]
                        };
    
    ItemsForSets.AddRange(result.ToList());
    
    // clean up results - preventing A:B and B:A from being considered unique,
    //    and ensuring all third ingredients actually contribute to a relationship.
    foreach (List<Item> items in ItemsForSets)
    {
        List<Quality> sharedQualities = Item.SharedQualities(this, items.ToArray());
        sharedQualities.Sort();
        List<String> sortedItems = items.ConvertAll(item => item.name); // I need the string names elsewhere 
        // TODO: I should rewrite to work directly with Items and convert after confirming I actually need the item.
        sortedItems.Sort(); // first part of preventing A:B B:A problems
        if (!Sets.ContainsKey(String.Join(", ", sortedItems))) // Dictionary provides second part.
        {
            if (items.Count == 3)
            {
                List<Quality> leftPairQualities = Item.SharedQualities(this, items.GetRange(0, 2).ToArray());
                leftPairQualities.Sort();
                if (leftPairQualities.SequenceEqual(sharedQualities))
                { // if the third item does not add a new quality
                    continue; // short circuit out to the next item
                }
            }
            // otherwise add to the list.
            Sets.Add(String.Join(", ", sortedItems), new Potion(items, sharedQualities));
        }
    }
    

    I can do more clean-up, and I can probably replace the foreach with another LINQ query but that dynamites the big roadblock and significantly improves performance.

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

Sidebar

Related Questions

I have a Dictionary object that is formed using a double as its key.
I have a Dictionary<int, string> which I want to take the Key collection into
Dictionary cannot have two values with same key. please tell which logic/algorithm being used
I have a Dictionary with key of type UInteger and the value is List(Of
I want to use a custom object as a Dictionary key, mainly, I have
I have a dictionary object in Visual Basic which I want to access with
I have a dictionary: dict = {key:[None, item 2, item 3]} How can I
I have a question about Javascript's dictionary. I have a dictionary in which key-value
I have dictionary which has {key : []} format. I need to prepare a
I have a dictionary: (key is 'name' and value is other dictionary) dictionary =

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.