views:

53

answers:

4

I have a small struct and I have to compare the values to find which ones have the same FreeFlow text, and then grab that struct ENumber.

   public struct Holder
    {
        public string FreeFlow;
        public int ENumber;
    }

and here is how I add them

foreach(Class1.TextElement re in Class1._TextElements)
            {
                //create struct with all details will be good for later
                Holder ph = new Holder();
                ph.FreeFlow = re.FreeFlow;
                ph.ENumber = re.ENumber;
                lstHolder.Add(ph);
            }

      foreach(Class1.TextElement2 re in Class1._TextElements2)
            {
                //create struct with all details will be good for later
                Holder phi = new Holder();
                phi.FreeFlow = re.FreeFlow;
                phi.ENumber = re.ENumber;
                lstHolder2.Add(phi);
            }

I can do a comparing using a foreach within a foreach, but I think this will not be the most effective way. Any help?

EDIT: I am trying to determine if freeflow text is exactly the same as the other struct freeflow text

A: 

What do you mean by "compare"? This could mean a lot of things. Do you want to know which items are common to both sets? Do you want to know which items are different?

LINQ might have the answer no matter what you mean. Union, Except, etc.

Bryan
I want to know what freeflow text are the same between sets, and then grab both numbers for each struct
Spooks
A: 

If you are using C# 3.0 or higher then try the SequenceEqual method

Class1._TextElements.SequenceEquals(Class1._TextElements2);

This will run equality checks on the elements in the collection. If the sequences are of different lengths or any of the elements in the same position are not equal it will return false.

JaredPar
+1  A: 

If I'm interpreting you correctly, you want to find the elements that are in both lstHolder and lstHolder2 - which is the intersection. If I'm interpreting correctly, then 2 step solution: first, override Equals() on your Holder struct. then use teh LINQ intersect operator:

var result = lstHolder.Intersect(lstHolder2);
Steve Michelotti
+1  A: 

I have to compare the values to find which ones have the same FreeFlow text, and then grab that struct ENumber.

If you can use LINQ you can join on the items with the same FreeFlow text then select the ENumber values of both items:

var query = from x in Class1._TextElements
            join y in Class1._TextElements2 on x.FreeFlow equals y.FreeFlow
            select new { xId = x.ENumber, yId = y.ENumber };

foreach (var item in query)
{
    Console.WriteLine("{0} : {1}", item.xId, item.yId);
}

EDIT: my understanding is the FreeFlow text is the common member and that ENumber is probably different, otherwise it would make sense to determine equivalence based on that. If that is the case the join query above should be what you need.

Ahmad Mageed