5

I'm really stumped by this one. I'm learning LINQ and used Microsoft Visual C# Express Edition to connect to a SQL Server database containing information about my books. I created the LINQ to SQL classes as needed. That stuff obviously works. It all works except I cannot figure out for the life of me why, if I search for "SR" (uppercase), it finds two records, "SR-71 Revealed, The Inside Story", as expected, but it also finds "Faded Sun: Kesrith" where the "sr" is lowercase. I'm using the IndexOf() method of the string class, which is supposed to perform a case-SENSITIVE comparison, right? The output displays the second book title as shown above, with the "sr" in lowercase. Here's the pertinent part of the code:

// normal using directives here
namespace QueryBooksDB {
    class Program {
        static void Main() {
            var urgh = new BooksDataContext();

            Console.WriteLine("Enter a string to search for:");
            string str = Console.ReadLine();

            var list = from book in urgh.Books
                        where book.Title.IndexOf(str) > -1
                        orderby book.Title
                        select new { ID = book.BookId, book.Title, book.Location };

            foreach ( var b in list ) {
                Console.WriteLine(b.Title);
            }
        }
    }
}
4

2 に答える 2

10

最後のステップで、クエリは sql に変換されます。SQL サーバーの文字列類似フィールド (varchar、nvarchar) では、大文字と小文字が区別されません。したがって select * from tbl where col like '%foo%'、値がまたはの場合に取得されFooますFOo

于 2011-10-25T17:52:36.020 に答える
2

デフォルトでは大文字と小文字が区別されると思っていましたが、いつでもStringComparisonオーバーロードを使用して大文字と小文字の区別を指定できます。

test.IndexOf("foo", StringComparison.Ordinal);

StringComparison列挙:

  1. 現在の文化
  2. CurrentCultureIgnoreCase
  3. 不変文化
  4. InvariantCultureIgnoreCase
  5. 序数
  6. 序数IgnoreCase
于 2011-10-25T17:55:27.790 に答える