0

I am displaying matching records of data using "Contains" linq property like this.

a=> a.user.displayname.contains(searchtext);

The result are coming prefect but some times a.user.displayname having more than 2 spaces then results are not coming.How to trim database data a.user.displayname .please tell me .

displayname :

venkatesh    duggirala,
mic   thomson

morespaces in database data.

Thanks, Venkat.

user3106578
  • 177
  • 1
  • 2
  • 12

3 Answers3

1

You can use replace it in your query

select string = replace(replace(replace(' select   single       spaces',' ','<>'),'><',''),'<>',' ')

It will replace multiple spaces into a single space in your query

Source:- Replace duplicate spaces with a single space in T-SQL

Similarly you can use in you linq too.
Can I use LINQ to strip repeating spaces from a string?

Or you can use Regular expression for that

myString = Regex.Replace(myString, @"\s+", " ");

Source:- How do I replace multiple spaces with a single space in C#?

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
1
a=> a.user.displayname.Replace(" ","").contains(searchtext);
shimron
  • 596
  • 6
  • 19
0

Setting up the query to try and compare with the additional spaces is possible. However I do not suggest that approach because by adding complex string manipulation into a Linq query it forces the C# side to pull the full dataset, then loop through the results, and run the additional comparisons that would ignore the extra whitespace.

With more than a thousand records this will become very memory and cpu intensive, and will saddle you with some very large scalability problems in the future. I would suggest that you fix the data with a sql query, and make sure that any imports clear the extra whitespace when it moves the data over.

Benji Vesterby
  • 574
  • 5
  • 21
  • i am using entity framework.i am not writing any query directly a.user.displayname then getting displayname result data. – user3106578 Dec 23 '13 at 06:31