Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-18
12
Medium Priority
?
1,367 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
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 

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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

718 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