0


这是我的问题。我们有一个企业、用户或任何东西的列表,我们必须使用“StartsWith”搜索类型对其进行搜索。所以在我们的网站上,我们没有像文本框这样的搜索字段,但我们有一个搜索标题,包括 27 个按钮“#”、“A”、“B”、“C”、[...]” Z”。

我们遇到的问题是,如果用户单击“E”按钮,当我们查询从数据库中获取值时,企业名称可能以“É”、“È”、“Ê”开头,因为是的,我们的网站是法语的. 关于如何在 LINQ 中执行此操作的任何想法。

知道我们正在使用 LLBLGen Pro 也很重要。所以我想它需要有一些东西他可以转换成一个有效的 SQL 查询。

这是我们已经尝试过的:

IList<Enterprise> 企业;
开关(搜索字符){
  [...]
  案例“E”:
     企业=来自ourContext.Enterprises中的ent
                   其中“eèéêë”.Any(param => ent.name[0] == param)
                   选择耳鼻喉科;
     休息;
  [...]
}

这给了我们这个错误相对于不可转换的查询:
Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'SD.LLBLGen.Pro.LinqSupportClasses.ExpressionClasses.SetExpression'.

因此,我们尝试通过简单的 LINQ 查询来实现它,而无需查询 DB 以了解它是否可能。

IList<string> test = new List<string>() { "École", "enlever", "avoir" };
IList<string> 结果 =(来自测试中的值
                        其中“eéèêë”.Contains(value[0].ToString())
                        选择值).ToList();

这个查询的奇怪之处在于它没有崩溃。但是,这也行不通!调试时,它会通过它,但是当我们尝试查看“结果”列表中的内容时,就好像里面什么都没有。我的意思是,列表只是空的。但是尝试捕获没有任何失败!

请帮忙 !

4

5 回答 5

1

The real solution is here is to create an extra column in your database for a searchable name, so whenever you add a new company you also add a value to the searchable name column. You would convert all characters to upper (or lower if you like) invariant and add the "clean" name to the searchable name column. You could also remove punctuation at this point too, as that is often a pain in searches.

This will leave you with a column you will never display but it will be much easier (and also much quicker) to search for matches in this column as you will never need to worry about accented characters at search time.

于 2011-03-28T14:12:24.647 回答
0

如果我在这里得到你就是你想要的:

var result = test.Where(x => "eéèêë".Contains(Char.ToLowerInvariant(x.FirstOrDefault())));
于 2011-03-28T13:56:43.483 回答
0

The Any seem to be not working. Use Contains instead. This is workin.

enterprises = from ent in ourContext.Enterprises
              where "eèéêëEÈÉÊËE".Contains(ent.name[0])
              select ent;
于 2011-03-28T14:13:44.997 回答
0

I'm not sure whether you have any control over the database, and which RDMBS you are using, but an easy way seems to be using a case insensitive, accent insensitive collation in your query - this way SQL does the hard work.

-- Assuming that your existing table is Accent Sensitive
create table Enterprises
(
name nvarchar(200) collate SQL_Latin1_General_CP1_CI_AS
)
GO

-- We can still use an Accent Insensitive View
create view vEnterprises
as
    select name collate SQL_Latin1_General_CP1_CI_AI AS name
        from Enterprises

GO

insert into Enterprises values ('e')
insert into Enterprises values ('è')
insert into Enterprises values ('é')
insert into Enterprises values ('ê')
insert into Enterprises values ('ë')

insert into Enterprises values ('E')
insert into Enterprises values ('É')
insert into Enterprises values ('È')
insert into Enterprises values ('Ê')


-- returns 'e' and 'E'
select * from Enterprises where name like 'e%'

-- returns everything
select * from vEnterprises where name like 'e%'

i.e. Assuming that you can add the accent insensitive view to LLBLGen you can just pass 'e' to the query.

于 2011-03-28T14:14:43.773 回答
0

只需使用字符串的 StartsWith 方法

IList<string> test = new List<string>() { "École", "enlever", "avoir" };

var result = test
    .Where(s => s.StartsWith("e", StringComparison.CurrentCultureIgnoreCase))
    .ToList();
于 2011-03-28T13:52:28.350 回答