Wolfy
Wolfy

Reputation: 4373

How to trim in Linq to SQL?

I have a table like this: (can't change this)

id  value   other
-----------------------------
1   000033  sasdsa
2   000033  dasfgds
3   33      sadasdas
4   33      pdfsfsd
5   234543  posjfd

Can someone tell me how can I trim leading zeros with Linq?

I tryed this:

from t in table
where  (t.value.TrimStart('0')).Equals("33")
select t

but it doesn't work :S

Upvotes: 2

Views: 3078

Answers (1)

Dave
Dave

Reputation: 3621

The 'value' field is obviously a string since it's retained the leading zeroes. I've not tried but can you not cast the value as an integer before doing the comparison? Something like:

from t in table
where (t => Convert.ToInt32(t) == 33)
select t

If you need to keep the value as a string, you could use string.EndsWith() but you might have problems with matching the correct 'value'.

Upvotes: 4

Related Questions