LINQ to Entities:为什么不能使用Split方法作为条件? [英] LINQ to Entities: Why can't I use Split method as condition?
问题描述
我有以下LINQ查询:
I have the following LINQ query:
var aKeyword = "ACT";
var results = from a in db.Activities
where a.Keywords.Split(',').Contains(aKeyword) == true
select a;
关键字是一个逗号分隔的字段.
Keywords is a comma delimited field.
每次我运行此查询时,都会出现以下错误:
Everytime I run this query I get the following error:
"LINQ to Entities无法识别方法'Boolean Contains [String](System.Collections.Generic.IEnumerable`1 [System.String],System.String)'方法,并且该方法无法转换进入商店表达式."
我要做什么呢?
推荐答案
针对大数据集上的性能考量:
In response to your performance considerations on a big dataset:
您将要在客户端上进行非索引通配符字符串匹配,因此,是的,会导致性能下降.
You are going to be doing non indexed wildcard string matching on the client, so yes, there will be performance loss.
在一个表字段中有多个关键字是有原因的吗?您可以将其归一化,以创建一个ActivityKeywords表,其中每个Activity都有许多Keyword记录.
Is there a reason why you have multiple keywords in one table field? You could normalize that out, to have a ActivityKeywords table where for each Activity you have a number of Keyword records.
活动(activity_id,.../*删除关键字字段*/)---> ActivityKeywords(活动ID,keyword_id)--->关键字(keyword_id,值)
Activities(activity_id, ... /* remove keywords field */) ---> ActivityKeywords(activity_id, keyword_id) ---> Keywords(keyword_id, value)
检出非第一范式: http://en.wikipedia.org/wiki/Database_normalization
同样,即使您只坚持一栏,也有一种方法可以在服务器端执行所有操作(如果您使用严格的语法:'keyword1,keyword2,...,keywordN'):
Also even if you were to stick with the one column, there is a way to do everything serverside (if you have a strict syntax: 'keyword1, keyword2, ..., keywordN'):
var aKeyword = "ACT";
var results = (from a in db.Activities
where a.Keywords.Contains("," + aKeyword) || a.Keywords.Contains(aKeyword + ",")
select a;
这篇关于LINQ to Entities:为什么不能使用Split方法作为条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!