x. Uniqueid) .Distinct()) No need to loop explicitly, manuall..." />

In C#, best way to check if stringbuilder contains a substring?

Personally I would use: return string. Join(",", RequestContext. Accounts .

Select(x => x. Uniqueid) .Distinct()) No need to loop explicitly, manually use a StringBuilder etc... just express it all declaratively :) (You'd need to call ToArray() at the end if you're not using . NET 4, which would obviously reduce the efficiency somewhat... but I doubt it'll become a bottleneck for your app.) EDIT: Okay, for a non-LINQ solution... if the size is reasonably small I'd just for for: First create a list of unique elements List ids = new List(); foreach (var account in RequestContext.

Accounts) { string id = account. Uniqueid; if (ids. Contains(id)) { ids.

Add(id); } } // Then convert it into a string. // You could use string. Join(",", ids.ToArray()) here instead.

StringBuilder builder = new StringBuilder(); foreach (string id in ids) { builder. Append(id); builder. Append(","); } if (builder.

Length > 0) { builder. Length--; // Chop off the trailing comma } return builder.ToString() If you could have a large collection of strings, you might use Dictionary.

Personally I would use: return string. Join(",", RequestContext. Accounts .

Select(x => x. Uniqueid) .Distinct()); No need to loop explicitly, manually use a StringBuilder etc... just express it all declaratively :) (You'd need to call ToArray() at the end if you're not using . NET 4, which would obviously reduce the efficiency somewhat... but I doubt it'll become a bottleneck for your app.) EDIT: Okay, for a non-LINQ solution... if the size is reasonably small I'd just for for: // First create a list of unique elements List ids = new List(); foreach (var account in RequestContext.

Accounts) { string id = account. Uniqueid; if (ids. Contains(id)) { ids.

Add(id); } } // Then convert it into a string. // You could use string. Join(",", ids.ToArray()) here instead.

StringBuilder builder = new StringBuilder(); foreach (string id in ids) { builder. Append(id); builder. Append(","); } if (builder.

Length > 0) { builder. Length--; // Chop off the trailing comma } return builder.ToString(); If you could have a large collection of strings, you might use Dictionary as a sort of fake HashSet.

In . Net 2.0? – kuul13 Feb 25 at 16:11 @user465876: You can, but personally I'd get hold of LINQBridge instead... LINQ is so useful, it's worth getting hold of the backport.

– Jon Skeet Feb 25 at 16:12 Jon, thanks for the tip. Soon we will be moving to 3.5 and then I will defiently use LINQ to the max. But for the timebeing, I need to stick to non-LINQ solution :( If you don't mind, can you tell me how to do this in 2.0 without LINQ/LINQBridge. – kuul13 Feb 25 at 16:35 @user465876: Okay, I've added an alternative for .

NET 2. – Jon Skeet Feb 25 at 16:39 thanks Jon. It is really helpful.

I will keep in mind about LINQ. – kuul13 Feb 25 at 17:00.

I have an existing StringBuilder object, the code appends some values and a delimiter to it. Now I want to modify the code to add the logic that before appending the text I want to check if it really exist in string builder variable or not? If not, then only append otherwise ignore.

What is the best way to do so? Do I need to change the object to string type? Need a best approach that would not hamper a performance.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions