您可以向实体框架中的实体添加“或”条件吗?例如是这样的:将实体框架中的实体添加或条件为
Property1 ==(1或2或3)
该消息把 “1 || 2 || 3” 或“1,2的值,当我,3" 或 “1或2或3” 返回此消息:
condition is not compatible with the type of the member
您可以向实体框架中的实体添加“或”条件吗?例如是这样的:将实体框架中的实体添加或条件为
Property1 ==(1或2或3)
该消息把 “1 || 2 || 3” 或“1,2的值,当我,3" 或 “1或2或3” 返回此消息:
condition is not compatible with the type of the member
你需要做的:
var results = entityCollection.Where(entity => entity.Property1 == 1 || entity.Property1 == 2 || entity.Property1 == 3);
还没有试过这个,但你可以尝试使用contains
。关于性能不知道,但代码更小:
int[] vals = new int[] { 1, 2, 3, 4 };
var results = entityCollection.Where(entity => vals.Contains(entity.Property1));
'Contains'仅在EF 4中支持,它还没有发布(它将在第12期发布.Net 4)。 MS网站上有一个候选版本。 – 2010-04-02 21:35:12
甚至没有想过 - 我一直在使用EF 4一段时间,所以这看起来很正常。 – 2010-04-03 00:47:47
您也应该检查出谓词建设者: http://www.albahari.com/nutshell/predicatebuilder.aspx
这是更先进一点,但如果你有动态链条件下它是你最好的选择。
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
我很害怕那个。我想要做的是在每次对该表执行查询时都会发生相同的情况,这样我就不必在每次打表时都写上述代码。 – 2010-04-02 19:52:11
@Blakewell:将查询放入实用程序类中,以便重用它。 – 2010-04-02 19:58:59
@Reed Copsey:你是否缺少一个'entity =>'? – 2010-04-02 19:59:46