Solved

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

Posted on 2013-12-18
12
1,007 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 74

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
 

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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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)…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now