1

在以下查询中

  var restrictions = from p in dcTrad.quop_restricted_items
                           where p.entry_status == 'P' && p.batch == "PRODUCTION" && p.problem != null
                           from q in dcTrad.model_companies
                           where   q.co_name != null && p.brimsec == q.primary_bsec                            
                           select new { Company = q.co_name, Restriction = p.comment ?? "Restricted without comments", Portfolio = p.problem };

我需要更换

p.brimsec == q.primary_bsec 

p.brimsec.StartsWith ( q.primary_bsec  )

但我收到以下错误:

Only arguments that can be evaluated on the client are supported for the String.StartsWith method

我怎样才能使这项工作?

4

3 回答 3

1

不幸的是,LINQ to SQL 翻译器不够聪明,无法翻译这段代码,但有一个技巧可以达到同样的效果:

p.brimsec.StartsWith ( q.primary_bsec  )

转换为:

p.brimsec.SubString(0, q.primary_bsec.Length) == q.primary_bsec

LINQ to SQL 翻译器处理得很好,语义等同于 StartsWith。

坦率地说,我不明白为什么为服务器端参数正确翻译 StartsWith 是如此困难,以至于 LINQ 开发人员只是决定抛出一个错误。

于 2010-12-13T19:54:12.867 回答
0

我认为您遇到的问题是 linq-to-sql 没有将 String.StartsWith 转换为 SQL。不过,String.Contains 确实有效 - 您需要检查生成的集合并过滤掉不以 q.primary_bsec 开头的项目。

于 2010-11-19T23:32:52.733 回答
0

基本上linq to sql 不知道如何将startswith 转换为Sql。这是因为在内部运行时您的 linq 是生成到 sql 的代码。

您可以通过创建 UDF(sql 中的用户定义函数)并在 linq 语句中使用它来实现这一点。

文章如下:http: //msdn.microsoft.com/en-us/library/bb399416.aspx

安德鲁

于 2010-11-19T23:36:19.703 回答