-1

I have (2) lists we shall call List A & List B which may be at different lengths. Each item has a contained parameter associated to it as an identifier, in this case we can call Comment. I need to iterate:

foreach (item a in A)
{
      foreach (item b in B)
      {
           if (b.Comment == a.Comment)
           {
            send to a void to process: void(b,a);

Essentially, I need to process each item from one list to the other if they have the same identifier. Would a zip benefit in this case? From what I've laid out, logically I would like to loop for each item in List A, check each item in List B that has the same identifier "Comment", if yes then send the current a & b value into a function to process and continue to loop the rest of List A.

Servy
  • 202,030
  • 26
  • 332
  • 449
AndyBernard
  • 105
  • 1
  • 9
  • 3
    I think you should look into the `Intersect` function of LINQ. See this Q&A [here](https://stackoverflow.com/questions/2129809/match-elements-between-2-collections-with-linq-in-c-sharp) that appears to be almost a perfect match of what you're trying to achieve. – gravity Dec 03 '18 at 20:34
  • Hey @gravitymixes thanks for the comment. Intersect is definitely a useful tool and could probably be used in a solution. However, finding similar values within 2 lists is only half the battle of what I am looking for. Correct me if I am wrong but Intersect just returns the common items within 2 lists. What I need is to take that a couple steps further. From (2) lists I need to process every item from List A that shares an identifier with List B and then process every item in the new List A with the new List B until all parameters are covered. – AndyBernard Dec 04 '18 at 14:11
  • @Servy marked this as a duplicate, and on review appears to have found an even better match. Let us know if you don't think this is a duplicate, and flag to `Reopen` the question after an [edit] describing why this isn't really a duplicate of that question. – gravity Dec 04 '18 at 14:52
  • Dear Community I am still in need of your help! Please review and let me know if you can provide some assistance. Thank you. – AndyBernard Dec 04 '18 at 18:55
  • @AndyBernard Can you edit the Question and provide the raw data set, and then what you expect the raw output data set to look like? Ignore the code, and focus on the input and expected output and I suspect we can help. – Andrew T Finnell Dec 04 '18 at 19:47
  • @AndrewTFinnell See updated post at end, I've highlighted an example of how the code would run. Thanks! – AndyBernard Dec 04 '18 at 20:40

2 Answers2

0

Rethink the collection type you are using.

If you had one of your lists as a dictionary, where the key is the comment, you would only have to iterate one of the lists, and then check if your dictionary contains that.

If comment is string, and item is a type.

Dictionary<string, item>

Using LINQ it's easy to generate a dictionary: https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.todictionary

Of course you can't have multiple items with the same key in the dictionary. If there is no unique constraint on comment, your dictionary could look like this

Dictionary<string, List<item>>

I won't go into more detail with the code without full context, but I think you get the point.

Peheje
  • 12,542
  • 1
  • 21
  • 30
  • Hey @Peheje, Thanks for the comment. I am familiar with the use of a dictionary but not for this case. I think I get what you are pointing at, that for a certain key/comment I can return a whole list at once to do something with. Is it possible to show me an example? – AndyBernard Dec 03 '18 at 20:37
-1

After some research I've went the Cartesian Product route obtaining each pair's respective [0] and [1] elements

var Fruits =
from apple in ListA
from Banana in ListB
select new[] {apple, Banana};

then went on to break down sending Smoothie(Fruits[0],Fruits[1])
AndyBernard
  • 105
  • 1
  • 9