1

I have a database created with a SQL_Latin1_General_CP1_CI_AS collation (which is case insensitive). I'm creating LINQ Expressions and trying to make "Contains" method which should compare strings in a case sensitive way.

I know that I can force collation if I use something like:

CHARINDEX(N'foo' COLLATE SQL_Latin1_General_CP1_CS_AS, 'something Foo') > 0

but since I'm building expressions using LINQ to create a query, my code looks like:

using System.Linq.Expressions;

private Expression Contains(Type type, string value, Expression propertyExpression)
    {
        var searchValue = Expression.Constant(value, typeof(string));
        var method = propertyExpression.Type.GetMethod("Contains", new[] { type });
        var result = Expression.Call(propertyExpression, method, searchValue);
        ...
    }

So, if I'm trying to use this Contains method for word 'foo', rows with 'Foo' will also be returned (which I don't want).

Is there a way that I can expand this Expression so that I can specify Case Sensitive comparison?

srdex
  • 76
  • 3
  • Does this answer your question? [Case insensitive string compare in LINQ-to-SQL](https://stackoverflow.com/questions/841226/case-insensitive-string-compare-in-linq-to-sql) – Selim Yildiz Nov 27 '19 at 08:45
  • Thanks but that question is about applying a case-insensitive comparison on a case-sensitive database - which is opposite of my case. – srdex Nov 27 '19 at 09:36
  • I do not believe it is possible to specify collation using EF. But you could join with a view that does it. – Magnus Nov 27 '19 at 13:18

0 Answers0