c# - Find first record based on start of string or until string is empty -


i have string lets this

var number = "02000123456"; 

and have list of records hold designation/number. mind has gone blank in trying figure out, want find first record match first 5 digits , if none, first 4 digits , if none, 3 digits , on.

i tried splitting first 5 digits array , doing query using array up, returning of matches.

i hoping doable without using loops. , using linq.

is possible write in single line or couple of lines without need write foreach/for loop.

basically want able go

var record = context.numbers.where(x => x.designation.startswith(number.substring(0,5)).firstordefault(). 

but being able change length of substring() until record populated or first 5 digits/characters have been exhausted.

the pure linq solution is

new []{5,4,3,2,1}.select(len => context.numbers.firstordefault(x => x.designation.startswith(number.substring(0, len)))).firstordefault(i => != null); 

the other base sequence initialization enumerable.range

enumerable.range(1, 5).reverse().select(len => context.numbers.firstordefault(x => x.designation.startswith(number.substring(0, len)))).firstordefault(i => != null); 

so it's naive solution, if have big dictionary need other solution solve issue.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -