RIA DomainService IQueryable - 选择
在学习了几个不同的教程之后,我一直在尝试针对我创建的数据库构建“Silverlight 业务应用程序”。我发现我有两个问题。我在这里问的是如何过滤查询。
使用 VS2010 模板时在 DomainService 中构建的查询是:
[EnableClientAccess]
public class ChargesService : LinqToEntitiesDomainService<ChargesEntities>
{
public IQueryable<tblChargeCode> GetCharges()
{
return ObjectContext.tblChargeCodes.OrderBy(e => e.Chrgs_Code_01).Take(10);
}
}
我正在尝试针对相同的 ObjectContext.tblChargeCodes 创建另一个查询。拉取整个表(30 列 x 约 7k 行)会产生超时错误。
我不知道如何进行选择。我想选择具有“开头”类型功能(动态下拉搜索功能)的 Charge_Codes_01 和 Bill_Description。我尝试过不同的变体,但没有成功。我脑子里有些东西不响。
public IQueryable<tblChargeCode> SearchCharges(string num)
{
var min = System.Convert.ToInt32(num.PadRight(7, '0'));
var max = System.Convert.ToInt32(num.PadRight(7, '9'));
return ObjectContext.tblChargeCodes
.Select(e => e.Chrgs_Code_01, e.Chrgs_Code_01_Desc)
.Where(e => e.Chrgs_Code_01 >= min && e.Chrgs_Code_01 <= max)
.OrderBy(e => e.Chrgs_Code_01)
.Take(10);
}
Following a couple different tutorials, I've been trying to build a "Silverlight Business Application" against a database I've created. I've found I have two problems. The one I'm asking about here is how to filter the query.
The query that is built in the DomainService is when using the VS2010 template is:
[EnableClientAccess]
public class ChargesService : LinqToEntitiesDomainService<ChargesEntities>
{
public IQueryable<tblChargeCode> GetCharges()
{
return ObjectContext.tblChargeCodes.OrderBy(e => e.Chrgs_Code_01).Take(10);
}
}
I'm trying to create another query against the same ObjectContext.tblChargeCodes. Pulling the entire table (30 columns by ~7k rows) creates a timeout error.
I can't figure out how to do a select. I want to select Charge_Codes_01 and Bill_Description with a "starts with" type functionality (dynamic drop down search functionality). I've tried different variations of this without success. Something just isn't clicking in my brain.
public IQueryable<tblChargeCode> SearchCharges(string num)
{
var min = System.Convert.ToInt32(num.PadRight(7, '0'));
var max = System.Convert.ToInt32(num.PadRight(7, '9'));
return ObjectContext.tblChargeCodes
.Select(e => e.Chrgs_Code_01, e.Chrgs_Code_01_Desc)
.Where(e => e.Chrgs_Code_01 >= min && e.Chrgs_Code_01 <= max)
.OrderBy(e => e.Chrgs_Code_01)
.Take(10);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(抱歉我的英语不好)
您的代码的问题是“Select” - 您的方法签名表示它必须返回 tblChargeCode 的 IQueryable,因此您无法返回投影。您可以通过以下两种方式编写查询:
在服务器中:
并在客户端上调用它:
或者您可以将查询留在服务器上:
并从客户端调用它(它将在服务器上过滤)
您还可以在查询中使用“Contains”而不是“StartsWith”。
(sorry for my bad english)
The problem with you code is the "Select" - your method signature says that it must return a IQueryable of tblChargeCode, so you cannot return a projection. Here is two ways you can write your query:
In the server:
And call it on the client:
Or you can just leave the query on the server:
And call it from the client (it will filter on the server)
You also can use "Contains" instead of "StartsWith" in your query.