0

Hi i have two string builders s1 and s2. I am assigning orders to that stringbuilder using comma separator. I want to compare two string builders. I want to know that all the orders in s1 are in s2 as well. If not i want know which order is missing in s2. How to achieve that?

if (!IsPostBack)
{
    int reccount = dsResult.Tables[0].Rows.Count;
    for (int count = 0; count < reccount;count++)
    {
        HashSet<string> arrayOrdId = new HashSet<string>();
        arrayOrdId.Add(dsResult.Tables[0].Rows[count][1].ToString());
        // arrayOrdId[count] = dsResult.Tables[0].Rows[count][1].ToString();
    }
}
radbyx
  • 9,352
  • 21
  • 84
  • 127
Philly
  • 251
  • 2
  • 5
  • 22

2 Answers2

7

I would suggest not using StringBuilder at all for this - instead, create two List<string> or possibly HashSet<string> to build up the order IDs, then compare those. If you need to create a string representation for other reasons, do that separately. (I'm assuming your order IDs are strings. If they're not, use the appropriate type of collection instead.)

It's not clear whether the order matters, but if it doesn't, HashSet<string> is what you should be using. You can find the differences easily enough:

var missingFromX = y.Except(x);
var missingFromY = x.Except(y);
// Do whatever you want with those differences
Jodrell
  • 34,946
  • 5
  • 87
  • 124
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That would certainly save the pointless step of splitting the strings on `','` and enable some nice set style linq. – Jodrell Aug 28 '12 at 16:18
  • 1
    @Jodrell: Indeed - it would also means keeping operations on the logical types. Extraneous string conversions are the bane of programming :) – Jon Skeet Aug 28 '12 at 16:19
  • how to add multiple orders to that hashset? – Philly Aug 28 '12 at 16:29
  • @user203127: Using the `Add` call. A `HashSet` is just another kind of collection... – Jon Skeet Aug 28 '12 at 16:30
  • after adding the first order, when try to insert second its replacing first order. see the code above. – Philly Aug 28 '12 at 16:35
  • 3
    @user203127 that's because you're recreating the `HashSet` each time the loop iterates; move the declaration outside of the loop. – sellmeadog Aug 28 '12 at 16:41
  • 1
    @user203127: What `sellmeadog` said, basically. It's very important to understand all the code you write - if you look through the code you've posted carefully, you should be able to understand that mistake. You'd see exactly the same kind of problem if you created the `StringBuilder` inside the loop. – Jon Skeet Aug 28 '12 at 16:42
1

Somthing like

var orders1 = new HashSet<string>();
var orders2 = new HashSet<string>();

// add the orders to the sets, not stringbuilders, here

var order1sInOrders2 = orders1.Intersect(orders2);
var order2sNotInOrders1 = orders2.Except(orders1);

You might want to consider doing this straight on IEnumerables of some custom Order class rather than strings.

Jodrell
  • 34,946
  • 5
  • 87
  • 124