none
LINQ query using "ToUpper()" not returning an expected value that IS in the table RRS feed

  • Question

  • I have a SQL Server table with color description in mixed case. I am using ToUpper to avoid case issues in the "where" clause, but it is not returning a result.

    For example, the color description contains "White" in the color_desc column, but does not return the record on the following query:

     t_stock_color col = (from c in ctxGE.t_stock_colors
                            where c.color_desc.ToUpper().Trim() == "WHITE"
                            select c).FirstOrDefault<t_stock_color>();

    What is the correct syntax for doing case-insensitive LINQ queries?

     

     

    Monday, January 26, 2009 12:47 AM

All replies

  • Nevermind... turned out to be a bad connection string to my DB.
    Monday, January 26, 2009 1:40 AM
  • Bill2010 said:

                            where c.color_desc.ToUpper().Trim() == "WHITE"



    Just a FYI - if your table has the potential of ever containing a lot of data you don't want to do this. SQL Server would not be able to use any indexes on that column. Using a case-insensitive collation or a computed+indexed column are alternative approaches.   (I'm assuming your real world scenario is not picking colors out of a color list).

    Monday, January 26, 2009 2:05 AM
    Answerer
  • Agreed. This is from an application that is doing a one-time conversion of customer data into customer databases (multi-tenent SaaS)... the business rules for this are rather involved as the source data is from an application that is decades old and data is messy and highly inconsistant; so C# and LINQ made sense insofar as speed is not necessarily a concern for this task as we are trying to clean it up as much as possible.
    Monday, January 26, 2009 5:23 PM