Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1449
  • Last Modified:

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

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
ToolTimeGang
Asked:
ToolTimeGang
  • 8
  • 3
1 Solution
 
13ShadowCommented:
Is your working field set as char(3) rather than varchar(3)?
0
 
käµfm³d 👽Commented:
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
 
ToolTimeGangAuthor Commented:
   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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
ToolTimeGangAuthor Commented:
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
 
käµfm³d 👽Commented:
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
 
ToolTimeGangAuthor Commented:
That still doesn't work.  I am going to stick with Exoas suggestion for now.  Thanks for trying to help!
0
 
käµfm³d 👽Commented:
I am going to stick with Exoas suggestion for now.
Huh?
0
 
ToolTimeGangAuthor Commented:
ok
0
 
ToolTimeGangAuthor Commented:
ok
0
 
ToolTimeGangAuthor Commented:
ok
0
 
ToolTimeGangAuthor Commented:
0
 
ToolTimeGangAuthor Commented:
Other suggestions were not working for me.  This one did work.  not the best solution, but it works.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now