Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I have a scenario where I have few strings in specific format.

Like ABC00001 and these are made incremented. like ABC0001, ABC0002 and upto ABC00000010

Mean number of zeroes incremented.
Well now situation is that I want a functionality where i would pass

00001 and 00000010 as parameters and query should give me all those records which have any of the string between this range at the end.


f => f.RegNo.Substring(8).CompareTo(param1) >= 0 && f.RegNo.Substring(8).CompareTo(param2



I have tried this but it does not work in this scenario, rather it works if both param length is same.
Posted
Comments
Sinisa Hajnal 29-Jan-15 6:14am    
Could you simply take last two characters and take everything that has 01-09 and 10 as the last two? You could int first; Integer.TryParse("0000001", out first); first should contain 1, same for second one...
Tomas Takac 29-Jan-15 6:20am    
Are the zeroes significant? In other words 00000010 == 10?
VICK 29-Jan-15 6:44am    
No. basically its a format for stock series. will start with 0000 and when reaches to 9999, than rather being going to 10000, it will restart with 000001. :(
BillWoodruff 29-Jan-15 8:22am    
"when reaches to 9999, than rather being going to 10000, it will restart with 000001"

Don't you mean that 10,000 would be 00001 ?
VICK 30-Jan-15 0:24am    
Typo Mistake. You are right Bill.

1 solution

Create your of comparer:
C#
class StockSeriesComparer : Comparer<string>
{
	public override int Compare(string x, string y)
	{
		if(x == null && y == null) return 0;
		if(x == null) return -1;
		if(y == null) return 1;
		
		if(x.Length < y.Length) return -1;
		if(x.Length > y.Length) return 1;
		
		return x.CompareTo(y);
	}
}

Then you can use it in your comparison like this:
C#
f => comparer.Compare(f, lowerBound) >= 0 && comparer.Compare(f, upperBound) <= 0;

This will work even without stripping the prefixes first, assuming they are all the same.
 
Share this answer
 
Comments
Maciej Los 12-Feb-15 8:33am    
Nice!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900