Solved

Is there a way I can trim trailing spaces in a linq query

Posted on 2013-12-18
12
1,176 Views
Last Modified: 2016-02-10
Is there a way I can trim trailing spaces in a linq query?

        query = From row In list
            Where row.Working = inputData

In the above example, row.working = "No " and inputData = "No".
I know the best fix is to not allow data into the db with trailing spaces, but for now, I need to work with the data I have.

I have tried trimend and contains and neither worked.  but maybe I am not using them correctly?  I am kind of a newbie with linq.

thank you for helping me!
0
Comment
Question by:ToolTimeGang
  • 8
  • 3
12 Comments
 
LVL 3

Expert Comment

by:13Shadow
ID: 39727982
Is your working field set as char(3) rather than varchar(3)?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39728000
Based on the code you show, I would expect:

query = From row In list
        Where row.Working.TrimEnd().ToUpper() = inputData.TrimEnd().ToUpper()

Open in new window

0
 

Author Comment

by:ToolTimeGang
ID: 39729171
   Private Function queryWorking(ByRef query As IEnumerable(Of ResponseTimeDto), _
                           list As List(Of ResponseTimeDto), _
                           inputData As String) As List(Of ResponseTimeDto)
        query = From row In list
            Where row.Working.TrimEnd().ToUpper() = inputData.TrimEnd().ToUpper()
        Return query.ToList()
    End Function

Open in new window

When I run this code, I get a nullreference exception on inputdata.trimend().toupper(). Object reference not set to an instance of an object.  Troubleshooting tip is to use the "new" keyword to create an object instance.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Accepted Solution

by:
ToolTimeGang earned 0 total points
ID: 39729291
Ok 13shadow and kaufmed.  I think I have a fix for this:
http://stackoverflow.com/questions/4685578/how-can-i-automatically-trim-whitespace-from-my-linq-queries

I used the fix that Exoas suggested.  It works for now until I have a more permanent solution for the data.

Make sense?  I still would like to fix it in the linq query example at the beginning of this post, but I can't get it to work.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39729448
When I run this code, I get a nullreference exception
You need to ensure that the strings you are trimming are not null. You might try:

query = From row In list
        Where row.Working.TrimEnd().ToUpper() = IF(inputData Is Nothing, String.Empty, inputData).TrimEnd().ToUpper()

Open in new window

0
 

Author Comment

by:ToolTimeGang
ID: 39735987
That still doesn't work.  I am going to stick with Exoas suggestion for now.  Thanks for trying to help!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39736069
I am going to stick with Exoas suggestion for now.
Huh?
0
 

Author Comment

by:ToolTimeGang
ID: 39736082
ok
0
 

Author Comment

by:ToolTimeGang
ID: 39736083
ok
0
 

Author Comment

by:ToolTimeGang
ID: 39736086
ok
0
 

Author Comment

by:ToolTimeGang
ID: 39736703
0
 

Author Closing Comment

by:ToolTimeGang
ID: 39743272
Other suggestions were not working for me.  This one did work.  not the best solution, but it works.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question