开发者

Entity Framework 4: SQL to Linq: How to select strings that are not uppercase?

开发者 https://www.devze.com 2023-02-04 09:43 出处:网络
This SQL selects all names that are not uppercase: select name frommyTable wherenot (UPPER(name) = name collate Latin1_General_BIN )

This SQL selects all names that are not uppercase:

select name
from   myTable
where  not (UPPER(name) = name collate Latin1_General_BIN )

How would you do the same think in Linq (Entity Framework 4?)

Edit:

Both String.Compare and strin开发者_运维百科g = string resolve to SQL of UPPER(name) = name which is case insensitive.


It's not pure LINQ, but you could use ExecuteQuery, eg

 string sql = @"
     select id , name 
     from   myTable
     where  not (UPPER(name) = name collate Latin1_General_BIN )
  ";

var results = 
   (from r in dc.ExecuteQuery<myTable>(sql) 
    select  new { r.Name } 
   ).ToList();

Note, you need to include id in the query, even if you only want name.


You can use the ToUpper() method to convert a string to upper case, e.g.

entities.Where(x => x.Name != x.Name.ToUpper()).Select(x => x.Name)

Obviously you should consider whether you need to supply a particular CultureInfo to the ToUpper() method and whether you want to do a culture specific comparison instead of using the != operator.


I don't exactly understand what you're trying to achieve.

Entity Framework and ADO.NET in general, have different patterns than SQL server. If you want to stick to the syntax in your query, create a Stored-Procedures or a UDF and port it to EF.

I tried to perform it with LINQ and I didn't find a way, since EF SQL generation for LINQ is case-insensitive! So I guess you'll have to do it with a UDF or a SPROC.

UPDATE:

I must leave now, but I think this post might help you: Unicode characters causing issues in SQL Server 2005 string comparison

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号