如何使用 Moles 通过 LINQ 从表中重定向选择?
我有一个名为“订阅”的表。我想将该表中的任何 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来我这样做完全错误。这是正确的方法:
使用此代码,订阅表中的任何选择都将仅返回这三个记录。
Looks like I was doing this completely wrong. This is the correct approach:
With this code, any selection from the Subscriptions table will return these three records only.