开发者

Entity Framework Contains/In Clause with SQLCE

开发者 https://www.devze.com 2023-01-28 13:37 出处:网络
I am trying to select items from an SQLCE table, where a field exists in a string array.This is easy in SQL with:

I am trying to select items from an SQLCE table, where a field exists in a string array. This is easy in SQL with:

SELECT * 
FROM TableX
WHERE SomeField In
([comma delimited array values]);

I am having a difficult time transposing this to LINQ. The following would logically work, but it is receiving this error: LINQ to Entities does not recognize the method 'Boolean Contains[String](System.Collections.Generic.IEnumerable`1[System.String], System.String)' method, and this method cannot be translated into a store expression.

var result = from c in DB.TableX
             where someStringArray.Contains(c.SomeField)
             select c;

Please let me know if anyone has any ide开发者_高级运维as or advice.

Thanks!

Update:

The following, reccomended below, throws a NotSupportedException, with error message, where class X is the class calling the enumeration: Unable to create a constant value of type 'NamespaceX.ClassX'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

var result = from c in DB.TableX
             where someStringArray.Any(s => s == c.SomeField)
             select c;


try

var result = from c in DB.TableX
             where someStringArray.Any(s => s == c.SomeField)
             select c;

Please mark as answer if this solves your problem.

0

精彩评论

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