开发者

LINQ to SQL (Executing Custom SQL Expressions) : how to substitute a parameter with an array value

开发者 https://www.devze.com 2023-02-17 23:54 出处:网络
Usage of method: DataContext.ExecuteQuery<TResult>(String, Object[]); The following generates an InvalidOperationException with message:

Usage of method:

DataContext.ExecuteQuery<TResult>(String, Object[]);

The following generates an InvalidOperationException with message:

{"Could not format node 'Value' for execution as SQL."}

int[] ids = new int[] {1, 2, 3};
context.ExecuteQuery<SourceTarget>(select c.* from Customer c where c.customer_id in {0}, ids);

Your help wi开发者_Go百科ll be really appreciated.


Since you're using LINQ-to-SQL, you could use the LINQ syntax instead of ExecuteQuery, eg:

var customers = from c in context.Customers
                where ids.Contains(c.customer_id)
                select c;

Or, if you insist on making it a SQL query, try:

context.ExecuteQuery<SourceTarget>(String.Format("select c.* from Customer c where c.customer_id in ({0})",
    String.Join(",", ids)));
0

精彩评论

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

关注公众号