您可以在实体框架中向实体添加“或”条件吗?例如:
Property1 ==(1或2或3)
将“1 || 2 || 3”或“1,2,3”或“1或2或3”的值放入时收到的消息会返回此消息:
condition is not compatible with the type of the member
答案 0 :(得分:12)
你需要这样做:
var results = entityCollection.Where(entity => entity.Property1 == 1 || entity.Property1 == 2 || entity.Property1 == 3);
答案 1 :(得分:10)
您还应该检查谓词构建器: http://www.albahari.com/nutshell/predicatebuilder.aspx
它有点先进,但如果你必须动态连锁条件,这是你最好的选择。
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or (p => p.Description.Contains (temp));
}
答案 2 :(得分:3)
没试过,但您可以尝试使用contains
。不确定性能,但代码较小:
int[] vals = new int[] { 1, 2, 3, 4 };
var results = entityCollection.Where(entity => vals.Contains(entity.Property1));