Link to home
Start Free TrialLog in
Avatar of ToolTimeGang
ToolTimeGangFlag for United States of America

asked on

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!
Avatar of 13Shadow
13Shadow

Is your working field set as char(3) rather than varchar(3)?
Avatar of kaufmed
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

Avatar of ToolTimeGang

ASKER

   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.
ASKER CERTIFIED SOLUTION
Avatar of ToolTimeGang
ToolTimeGang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

That still doesn't work.  I am going to stick with Exoas suggestion for now.  Thanks for trying to help!
I am going to stick with Exoas suggestion for now.
Huh?
ok
ok
ok
Other suggestions were not working for me.  This one did work.  not the best solution, but it works.