Linq 相当于 SQL LEFT 函数?

发布于 2024-11-05 22:21:39 字数 501 浏览 0 评论 0原文

我们有一个数据库,其中一些字段为 varchar(max),可能包含大量文本,但是我遇到的情况是,我只想从字段中选择前 300 个字符,用于 MVC 网站上的分页结果表进行该领域的“预览”。

对于一个简化的示例查询,我想在表中显示所有位置 (这将被分页,所以我不只是得到所有的东西 - 我一次可能得到 10 个结果):

return db.locations;

然而,这给了我一个位置对象,其中所有字段都包含大量文本,执行起来非常耗时。

因此,我之前采取的方法是使用 SQL 存储过程

LEFT(field, 300) 

来解决此问题,然后在 Linq to SQL .dbml 文件中包含存储过程以返回结果的“位置”对象。

但是我有很多查询,我不想为每个查询都执行此操作。

这可能是一个简单的解决方案,但我不确定如何在搜索引擎上表达这个问题,我将不胜感激任何可以帮助我解决这个问题的人。

We have a database with some fields that are varchar(max) which could contain lots of text however I have a situation where I only want to select the first for example 300 characters from the field for a paginated table of results on a MVC web site for a "preview" of the field.

for a simplified example query where I want to get all locations to display in the table
(this would be paginated, so I don't just get everything - I get maybe 10 results at a time):

return db.locations;

However this gives me a location object with all the fields containing the massive amounts of text which is very time consuming to execute.

So what I resorted to before was using SQL stored procedures with the:

LEFT(field, 300) 

to resolve this issue and then in the Linq to SQL .dbml file included the stored procedure to return a "location" object for the result.

However I have many queries and I don't want to have to do this for every query.

This maybe a simple solution, but I am not sure how I can phrase this on a search engine, I would appreciate anyone who can help me with this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江城子 2024-11-12 22:21:39

您也可以使用直接转换为这些函数的函数,当您需要转换在 SQL 中功能正常且在 LINQ 中没有风险的代码时,这非常有用。
看一下 System.Data.Objects.EntityFunctions

Locations.Select(loc=>System.Data.Objects.EntityFunctions.Left(loc.Field,300))

这将在服务器端直接转换为 LEFT

You can use functions that directly translate to those functions too, this is useful when you need to translate code that functionally works just fine in SQL at no risk in LINQ.
Have a look at System.Data.Objects.EntityFunctions

Locations.Select(loc=>System.Data.Objects.EntityFunctions.Left(loc.Field,300))

This will get directly translated into a LEFT on the server side.

猫性小仙女 2024-11-12 22:21:39

编辑:我将 LEFT 误读为 LTRIM。以下是所有不能在 LINQ to SQL 中使用的字符串函数 。您尝试过String.Substring()吗?

最好的选择是映射存储过程并继续使用它。 这是一篇带有屏幕截图的精彩文章,向您展示了如何执行此操作。

如果您不使用设计器工具 您还可以针对 DataContext 调用 ExecuteCommand。它并不漂亮,但这就是我们现在所拥有的。

EDIT: I misread LEFT for LTRIM. Here's all the String functions that can't be used in LINQ to SQL. Have you tried String.Substring()?

Your best option is to map the stored procedure and continue using it. Here is an excellent article with screen shots showing you how to do so.

If you're not using the designer tool you can also call ExecuteCommand against the DataContext. It isn't pretty, but it's what we have for now.

窝囊感情。 2024-11-12 22:21:39

我发现这样的东西对我有用:

return from locationPart in db.locations
       select new LocationPart
       {                       
         Description = locationPart.description,
         Text = locationPart.text.Substring(0,300)                       
       };

不理想,因为我必须使用“选择新”来返回不同的对象,但它似乎有效。

I found something like this worked for me:

return from locationPart in db.locations
       select new LocationPart
       {                       
         Description = locationPart.description,
         Text = locationPart.text.Substring(0,300)                       
       };

Not ideal because I have to use "select new" to return a a different object, but it seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文