1

I have a linq query

var x = c from context.customers
where c.Name == ''

and

var x =  c from context.customers
where c.Name == ' '

I have a customername column in SQL 2008 database, with varchar(50) and one customer is blank and other has a space, but the TSQL generated by Linq is right but return for both queries return both customers and not 1 record which I want, which is either blank or has a whitespace. I know the underlying problem is a SQL server issue as per this question asked.

SQL Server 2008 Empty String Vs Space

I want to modify my Linq query. how can I do this? without using SQL server T-SQL commands in linq.

Community
  • 1
  • 1
chugh97
  • 9,602
  • 25
  • 89
  • 136

1 Answers1

2

If you are using Entity Framework then make use of SqlFunctions.DataLength to distinguish between an empty string and white-space.

e.g

var x = c from context.customers
where c.Name == '' && SqlFunctions.DataLength(c.Name) == 0

and

var x = c from context.customers
where c.Name == '' && SqlFunctions.DataLength(c.Name) != 0

SqlFunctions.DataLength translates to the TSQL DATALENGTH function. DATALENGTH will give you the number of bytes a string requires including any white-space. By comparison the LEN function will give you the number of characters after trimming any trailing white-space.

Phil
  • 42,255
  • 9
  • 100
  • 100