RIA DomainService IQueryable - 选择

发布于 2024-12-13 02:30:12 字数 1115 浏览 3 评论 0原文

在学习了几个不同的教程之后,我一直在尝试针对我创建的数据库构建“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 技术交流群。

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

发布评论

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

评论(1

柠北森屋 2024-12-20 02:30:12

(抱歉我的英语不好)

您的代码的问题是“Select” - 您的方法签名表示它必须返回 tblChargeCode 的 IQueryable,因此您无法返回投影。您可以通过以下两种方式编写查询:

在服务器中:

public IQueryable<tblChargeCode> SearchCharges(int min, int max, string description)
{        

    return ObjectContext.tblChargeCodes            
        .Where(e => e.Chrgs_Code_01 >= min && e.Chrgs_Code_01 <= max)
        .Where(e => e.Bill_Description.StartsWith(description))
        .OrderBy(e => e.Chrgs_Code_01)
        .Take(10);
}

并在客户端上调用它:

context.Load(context.SearchChargesQuery(0, 9999999, "Bill"), (op) => 
{
    //op.Entities has your entities loaded
}, null);

或者您可以将查询留在服务器上:

public IQueryable<tblChargeCode> GetCharges()
{
    return ObjectContext.tblChargeCodes.OrderBy(e => e.Chrgs_Code_01);
}

并从客户端调用它(它将在服务器上过滤)

context.Load(context.GetChargesQuery().Where(e => e.Chrgs_Code_01 >= 0 && e.Chrgs_Code_01 <= 9999999)
        .Where(e => e.Bill_Description.StartsWith("Bill"))
        .OrderBy(e => e.Chrgs_Code_01)
        .Take(10), (op) => 
        {
            //op.Entities has your entities loaded
        }, null);

您还可以在查询中使用“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:

public IQueryable<tblChargeCode> SearchCharges(int min, int max, string description)
{        

    return ObjectContext.tblChargeCodes            
        .Where(e => e.Chrgs_Code_01 >= min && e.Chrgs_Code_01 <= max)
        .Where(e => e.Bill_Description.StartsWith(description))
        .OrderBy(e => e.Chrgs_Code_01)
        .Take(10);
}

And call it on the client:

context.Load(context.SearchChargesQuery(0, 9999999, "Bill"), (op) => 
{
    //op.Entities has your entities loaded
}, null);

Or you can just leave the query on the server:

public IQueryable<tblChargeCode> GetCharges()
{
    return ObjectContext.tblChargeCodes.OrderBy(e => e.Chrgs_Code_01);
}

And call it from the client (it will filter on the server)

context.Load(context.GetChargesQuery().Where(e => e.Chrgs_Code_01 >= 0 && e.Chrgs_Code_01 <= 9999999)
        .Where(e => e.Bill_Description.StartsWith("Bill"))
        .OrderBy(e => e.Chrgs_Code_01)
        .Take(10), (op) => 
        {
            //op.Entities has your entities loaded
        }, null);

You also can use "Contains" instead of "StartsWith" in your query.

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