Thursday, 15 August 2013

c# - Linq Query filtering results -



c# - Linq Query filtering results -

i'm not super familiar linq. here's code:

if (!string.isnullorempty(searchstring)) { games = games.where(x => x.name.contains(searchstring)); }

works should; however, i'd like, rather filtering results based on whatever text user inputs, if user inputs "l" there way within linq query have filter through 1st letters of x.name verses x.name contains letter.

i.e. have...

league_of_legends lotro_ dota_2 call_of_duty_4 final_fantasy_7

and user inputs "l" rather returning dota 2, can homecoming league of legends , lotro 2 who's names start "l".

i'm assuming along lines of... games = games.where(x => x.name.contains<***something***>(searchstring));

thanks!

i assume you're looking startswith. note work prefix, not single letter.

games = games.where(x => x.name.startswith(searchstring));

if want case-insensitive search (e.g. lowercase l matches league_of_legends too), use:

games = games.where(x => x.name.startswith(searchstring, stringcomparison.currentcultureignorecase));

c# linq filter asp.net-mvc-5

No comments:

Post a Comment