Linq是否等效于SQL LEFT函数? [英] Linq equivalent of SQL LEFT function?

查看:136
本文介绍了Linq是否等效于SQL LEFT函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个数据库,其中的某些字段为varchar(max),其中可能包含很多文本,但是我遇到的情况是,我只想从该字段中选择第一个字符(例如300个字符),以对页面上的结果进行分页MVC网站提供该字段的预览".

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.

对于简化的示例查询,我要在其中获取所有要显示在表中的位置 (这将是分页的,所以我不仅得到所有信息-一次可能得到10个结果):

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.

所以我之前所求助的是使用带有以下内容的SQL存储过程:

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

LEFT(field, 300) 

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

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.

推荐答案

我误读了LTRIMLEFT.这是在LINQ to SQL中不能使用的字符串函数 .您是否尝试过String.Substring()?

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.

这篇关于Linq是否等效于SQL LEFT函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆