开发者

Linq to SQL Join and Contains Operators

in the following query

  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 };

I need to replace

p.brimsec == q.primary_bsec 

with

p.开发者_如何学编程brimsec.StartsWith ( q.primary_bsec  )

but I get the following error:

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

How can I make this work?


Unfortunately the LINQ to SQL translator is not smart enough to translate this code, but there's a trick that achieves the same:

p.brimsec.StartsWith ( q.primary_bsec  )

Translates to:

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

The LINQ to SQL translator handles this just fine, and the semantics are equivalent to StartsWith.

Frankly, I don't see why translating StartsWith properly for server-side arguments was so hard that the LINQ developers just decided to throw an error instead.


Basically the linq to sql does not know how to convert startswith to Sql. This is because internally at run time your linq is code generated to sql.

You may go about achieving this by creating a UDF (user defined function in sql) and using it from your linq statement.

The article is as below: http://msdn.microsoft.com/en-us/library/bb399416.aspx

Andrew


I think the problem you're running into is that linq-to-sql has no translation of String.StartsWith into SQL. String.Contains does work, though - you'd need to go through your resultant collection and filter out the items that don't start with q.primary_bsec.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜