Reputation: 251
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();
}
}
Upvotes: 0
Views: 630
Reputation: 1502816
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
Upvotes: 7
Reputation: 35726
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 IEnumerable
s of some custom Order
class rather than string
s.
Upvotes: 1