Solved

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

Posted on 2013-12-18
12
1,271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

631 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