如何使用 Moles 通过 LINQ 从表中重定向选择?

发布于 2024-10-08 13:05:58 字数 1236 浏览 0 评论 0原文

我有一个名为“订阅”的表。我想将该表中的任何 LINQ 选择重定向到 Moles lambda,以便从该表中只返回 3 行——基本上我想绕过对数据库的调用。到目前为止,我的代码如下所示:

// lazy loader is here to handle successive calls to the 
// same table (otherwise there's an error)
// CM is a namespace alias
Table<CM.Subscriptions> subsTable = null;
MTheDataContext.AllInstances.SubscriptionsGet = (c) =>
    {
        if (subsTable == null)
        {
            subsTable = c.GetTable<CM.Subscriptions>();
            subsTable.Attach(new CM.Subscriptions() { SubID = 1, 
                 StatusCode = 1, CustomerID = custID1 });
            subsTable.Attach(new CM.Subscriptions() { SubID = 2, 
                 StatusCode = 1, CustomerID = custID2 });
            subsTable.Attach(new CM.Subscriptions() { SubID = 3, 
                 StatusCode = 4, CustomerID = custID3 });
            //  c.Refresh(RefreshMode.KeepCurrentValues, t);
        }
        return subsTable;
    };

不幸的是它不起作用。我在数据库的订阅表中有大约 1000 行。当我运行一些包含此重定向的测试代码时,我从数据库中获取 1000 行,而不是重定向方法中的 3 行。显然我错过了一些东西。每当从订阅中选择任何测试代码时,如何才能仅返回这 3 行?我对 3 个不同的表进行了 3 次调用,它们都需要选择数据库中没有的数据才能使此测试正常工作。

澄清:当我执行 from sub in dc.Subscriptions ... select 时,确实会调用重定向方法。但返回的行不是重定向中的行。

I have a table called Subscriptions. I'd like to redirect any LINQ select from that table to a Moles lambda so that only 3 rows are returned from that table -- basically I want to bypass the call to the database. So far, the code I have looks like this:

// lazy loader is here to handle successive calls to the 
// same table (otherwise there's an error)
// CM is a namespace alias
Table<CM.Subscriptions> subsTable = null;
MTheDataContext.AllInstances.SubscriptionsGet = (c) =>
    {
        if (subsTable == null)
        {
            subsTable = c.GetTable<CM.Subscriptions>();
            subsTable.Attach(new CM.Subscriptions() { SubID = 1, 
                 StatusCode = 1, CustomerID = custID1 });
            subsTable.Attach(new CM.Subscriptions() { SubID = 2, 
                 StatusCode = 1, CustomerID = custID2 });
            subsTable.Attach(new CM.Subscriptions() { SubID = 3, 
                 StatusCode = 4, CustomerID = custID3 });
            //  c.Refresh(RefreshMode.KeepCurrentValues, t);
        }
        return subsTable;
    };

Unfortunately it doesn't work. I've got about 1000 rows in the Subscriptions table in the database. When I run some test code that has this redirect in it, I get the 1000 rows from the database instead of the 3 rows that are in the redirect method. Clearly I'm missing something. What can I do to return only these 3 rows whenever any test code selects from the Subscriptions? I've got 3 calls to 3 different tables and they all need to select data that isn't in the db to get this test to work.

Clarification: the call to the redirected method does happen when I do a from sub in dc.Subscriptions ... select. But the rows returned aren't the rows that are in the redirect.

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

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

发布评论

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

评论(1

吻泪 2024-10-15 13:05:58

看起来我这样做完全错误。这是正确的方法:

// using System.Data.Linq.Moles; 
// CM is a namespace alias 
var subsList = new List<CM.Subscription>{
  new CM.Subscription() { SubscriptionID = subId1 },
  new CM.Subscription() { SubscriptionID = subId2 },
  new CM.Subscription() { SubscriptionID = subId3 }};

var subsTable = new MTable<CM.Subscription>();

subsTable.Bind(subsList.AsQueryable());

MTheDataContext.AllInstances.SubscriptionGet = (c) => { return subsTable; };

使用此代码,订阅表中的任何选择都将仅返回这三个记录。

Looks like I was doing this completely wrong. This is the correct approach:

// using System.Data.Linq.Moles; 
// CM is a namespace alias 
var subsList = new List<CM.Subscription>{
  new CM.Subscription() { SubscriptionID = subId1 },
  new CM.Subscription() { SubscriptionID = subId2 },
  new CM.Subscription() { SubscriptionID = subId3 }};

var subsTable = new MTable<CM.Subscription>();

subsTable.Bind(subsList.AsQueryable());

MTheDataContext.AllInstances.SubscriptionGet = (c) => { return subsTable; };

With this code, any selection from the Subscriptions table will return these three records only.

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