Solved

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

Posted on 2013-12-18
12
1,109 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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