Linq 查询 连接具有不同数据源的对象?
我正在尝试连接两个对象,第一个是在 Helper 中定义的(静态)本地对象,第二个是(亚音速)数据库对象。
这是我的存储库中令人不快的摘录,除非有要求,否则我不会用模型和助手让您感到厌烦。
公共 IQueryable GetData(字符串数据类型) {
IQueryable<DatabaseObject> datalist = (
from t in db.All<DatabaseObject>()
join e in WebHelpers.LocalList.AsQueryable<LocalObject>()
on t.Type equals e.Type
orderby t.DateOccurred descending
select t
).Where(e => e.Category == TransType);
return datalist;
}
我意识到,通过将此表放入数据库,我的生活可以轻松 1000 倍,并且对于下一个版本,我很可能会这样做。但有没有办法实现我想要做的事情呢?我认为这是(a)我没有返回正确的数据类型,因为视图模型期望 IQueryable 或(b)Subsonic 导致了问题。
I am trying to join two objects, the first is a (static) local object defined in a Helper and the second (Subsonic) Database object.
Here is the offending extract from my repository, I wont bore you with the models and helpers unless requested.
public IQueryable GetData(string DataType)
{
IQueryable<DatabaseObject> datalist = (
from t in db.All<DatabaseObject>()
join e in WebHelpers.LocalList.AsQueryable<LocalObject>()
on t.Type equals e.Type
orderby t.DateOccurred descending
select t
).Where(e => e.Category == TransType);
return datalist;
}
I realise that I could me my life 1000 times easier by putting this table into the database, and for the next release I may very well do this. But is there a way to achieve what I am trying to do? Im thinking this is either (a) Im not returning the correct datatype as the view model expects IQueryable or (b) Subsonic is causing the issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕 SubSonic 不支持这种交叉引用(至少我上次尝试时不支持)。
您可以通过仅使用 SubSonic 从数据库中检索必要的数据来解决此问题,然后在正常的 C# Linq-to-Objects 中执行 Join:(
顺便说一下,如果您移动
.Where()
位于orderby
之上,因为这样需要排序的内容就更少了,也许您甚至可以将其包含在数据库查询中(检索dbData
的查询)。 )我不太清楚,因为你的代码很混乱,因为它引用了你没有在任何地方声明的TransType
,并且该方法有一个参数DataType
,但它不是在任何地方使用,并且t
和e
的使用不一致。)I am afraid SubSonic doesn’t support this kind of cross-referencing (at least it didn’t last time I tried).
You can work around this by using SubSonic only to retrieve the necessary data from the database, but then do the Join in normal C# Linq-to-Objects:
(By the way, you can probably achieve better performance if you move the
.Where()
to above theorderby
because then there is less to sort. Perhaps you can even include it in the database query (the one that retrievesdbData
). I can’t quite tell because your code is confusing because it references aTransType
which you haven’t declared anywhere, and the method has a parameterDataType
which is not used anywhere, and you uset
ande
inconsistently.)我的最终解决方案是重新组织 where 子句,以确保 select 仅返回正确的数据类型 (IQueryable)。在不重新组织 where 子句的情况下,尽管指定了 select t,连接数据仍会通过对象传递。
My final solution was to reorganise the where clauses to ensure that the select returns only the correct datatype (IQueryable). Without reorganising the where clauses, the join data is still passed through the object despite specifying select t.