Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So it sounds like you want a bunch of linkedlists, where each linked list contains a user's choices made in chronological order. Furthermore, you want to be able to access all user choices for a specific year. So the way I've done it below first builds a list of linkedlistnode objects (NOT a linkedlist), then uses linq to build a dictionary of these nodes keyed by year.</p> <p>I've made an assumption that only one change per user is recorder per year.</p> <pre><code> var TheActualChoiceIsIrrelevantForThisExample = new Choice(); var Ditto = new Choice(); var userChoices = new List&lt;UserChoice&gt; { new UserChoice("2000", 1, TheActualChoiceIsIrrelevantForThisExample ), new UserChoice("2000", 2, Ditto ), new UserChoice("2000", 3, Ditto ), new UserChoice("1999", 1, Ditto ), new UserChoice("1999", 2, Ditto ), new UserChoice("1999", 3, Ditto ), new UserChoice("2001", 1, Ditto ), new UserChoice("2001", 2, Ditto ), new UserChoice("2001", 3, Ditto ), }; var userChoicesGroupedById = from userChoice in userChoices group userChoice by userChoice.UserId; /*using List of LinkedListNode allows linq queries to access the nodes' previous and next properties. * if a linkedlist was used, then these properties would not be accessible (because we would be querying UserChoice objects, not LinkedListNodes) */ var linkedUserChoices = new List&lt;LinkedListNode&lt;UserChoice&gt;&gt;(); foreach (var grp in userChoicesGroupedById) { var userChoicesSortedByYear =new LinkedList&lt;UserChoice&gt;( grp.OrderBy(userChoice=&gt;userChoice.Year)); var currentNode = userChoicesSortedByYear.First; while (currentNode != null) { linkedUserChoices.Add(currentNode); currentNode = currentNode.Next; } } var userChoicesGroupedByYear = (from userChoiceNode in linkedUserChoices group userChoiceNode by userChoiceNode.Value.Year).ToList(); var dictionary = userChoicesGroupedByYear.ToDictionary(group =&gt; group.Key, group =&gt; group.ToList()); </code></pre> <p>here are the class definitions for the objects I've used:</p> <pre><code>class UserChoice { public string Year { get; set; } public Choice Choice { get; set; } public int UserId { get; set; } public UserChoice(string year, int userId, Choice choice ) { Year = year; Choice = choice; UserId = userId; } } class Choice { } </code></pre> <p>Once the final dictionary is built it can be used like: </p> <pre><code>dictionary["2000"] .Single(node=&gt;node.Value.UserId == 42) .Next //forward one node - i.e. the next year that the user made a choice. Not neccessarily the next calendar year. .Previous.Previous.Previous //back three nodes //etc </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload