LinqProvider 从两个地方获取数据并合并它们
我有一个类似于以下内容的 Linq 查询
var query3 = from c in Session.CreateLinq<AccountTransaction>()
join a in Session.CreateLinq<Account>() on c.Account equals a
where c.DebitAmount >= 0
select new { a.Name, c.DebitAmount }
;
Session 对象在幕后与数据源交互,但它也有一个可能会发生变化的内部缓存状态。当我运行查询时,我想查询内部缓存状态和数据源,然后将结果合并在一起,其中内部缓存状态优先。
我正在使用 re-linq 来生成针对数据源的查询,该查询工作正常。我不确定如何使用相同的 Linq 查询对内部状态进行查询。
如果我只想查询内部状态,可以使用 Session 上的 GetAllCached() 调用来代替 Session.CreateLinq。但我不确定在我的自定义提供程序中的哪个点我可以使用 GetAllCached() 处理向数据源和内部状态的移交。
感谢任何 Linq 专家的任何建议。
I have a Linq query that looks something like the following
var query3 = from c in Session.CreateLinq<AccountTransaction>()
join a in Session.CreateLinq<Account>() on c.Account equals a
where c.DebitAmount >= 0
select new { a.Name, c.DebitAmount }
;
The Session object interacts with a datasource behind the scenes but it also has an internal cached state which may have changes. When I run a query I would like to query the both the internal cached state AND the datasource and then merge the results together, with the internal cached state taking precendence.
I am using re-linq for the generation of the query against the datasource which is working fine. What I am not sure about is how to also do the query against the internal state using the same Linq query.
There's a call GetAllCached() on Session that I can use instead of Session.CreateLinq if I just wanted to query the internal state. But I'm not sure at which point in my custom provider I can handle handing off to the datasource AND the internal state using GetAllCached().
Any suggestions appreciated from any Linq gurus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修改时间:新加坡时间 04:51 AM
Modified: 04:51 AM Singapore Time
如果我理解正确的话,您的数据源有一个自定义 LINQ 提供程序,并且还有一种获取缓存结果的(可能是类型安全的)方法。
在这种情况下,我建议仅使用 LINQ to Objects 来访问缓存集。您可以使用
AsEnumerable
从自定义 LINQ 提供程序“跳出”到 LINQ to Objects。不过,
join
带来了一个问题。由于这些类型中的任何一种都可能存在于缓存中,因此不可能将逻辑推送到数据库。例如,是否可以在缓存中有一个AccountTransaction
而其Account
也在缓存中?如果您允许缓存中出现任何情况(例如,
AccountTransaction
没有关联的Account
记录),那么您必须在内存中而不是在数据库中进行联接:但是,如果您可以更好地控制缓存,并且仅在关联的
Account
对象存在时才允许AccountTransaction
对象存在,然后您可以推送join
对数据源进行操作并在内存中执行另一操作,合并结果:我认为。我不是 LINQ 方面的专家,所以我很想看到其他回复。
If I understand correctly, you have a single custom LINQ provider for your datasource, and a (presumably type-safe) way of getting cached results as well.
In this case, I recommend just using LINQ to Objects to access your cached set. You can use
AsEnumerable
to "step out" of your custom LINQ provider into LINQ to Objects.The
join
brings up a problem, though. Since either of these types may exist in the cache, it's not possible to push logic to the DB. For example, is it possible to have anAccountTransaction
in the cache without itsAccount
also being in the cache?If you allow any situation in the cache (e.g.,
AccountTransaction
without associatedAccount
records), then you have to do the join in memory and not in the db:However, if you have more control over your cache, and only allow
AccountTransaction
objects to be present if their associatedAccount
objects are present, then you can push thejoin
operation to the datasource and do another one in memory, merging the results:I think. I am not an expert in LINQ, so I'm curious to see other responses.