组合 2 个 IQueryable 结果
我发现了一些看起来相似但不完全相同的问题,所以我会继续回答。
我正在使用 EF 检索 2 个表。这些表/实体没有真正的“是”关系。它们恰好有几个公共字段,我想在包含两个表之间的组合的前 N 个条目的列表中公开这些公共字段。每行都必须有某种方法来识别它是什么类型以及它指向哪个实例。
我已经解决了这个问题,但我想我想知道是否有更好的方法。我的解决方案是创建一个 ViewModel 类:
intenal class EntityAEntityBCombination
{
public int? EntityAID { get; set; }
public int? EntityBID { get; set; }
public string CommonProperty { get; set; }
}
然后我这样做了:
var results = (
from a in EntityAList select new EntityAEntityBCombination
{ EntityAID = a.Id, EntityBID = null, CommonProperty = a.CommonProperty }
).Concat(
from b in EntityBList select new EntityAEntityBCombination
{ EntityAID = null, EntitiyBID = b.Id, CommonProperty = b.CommonProperty }
).Fetch(N)
它可以工作,但看起来很脏。有什么建议吗?
I found some questions that looked similar, but not exactly the same so I'll go for it.
I'm using EF to retrieve 2 tables. These tables/entities have no real "is a" relationship. They just happen to have a couple of common fields which I want to expose in a list containing the first N entries of a combination between the two tables. Each row has to have some way of identifying which type it is and which instance it points to.
I have solved the problem, but I guess I was wondering if there was a better way. My solution was to create a ViewModel class:
intenal class EntityAEntityBCombination
{
public int? EntityAID { get; set; }
public int? EntityBID { get; set; }
public string CommonProperty { get; set; }
}
Then I did this:
var results = (
from a in EntityAList select new EntityAEntityBCombination
{ EntityAID = a.Id, EntityBID = null, CommonProperty = a.CommonProperty }
).Concat(
from b in EntityBList select new EntityAEntityBCombination
{ EntityAID = null, EntitiyBID = b.Id, CommonProperty = b.CommonProperty }
).Fetch(N)
It works, but seems dirty. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个,也许它不能直接开箱即用,但它应该给你一个想法:
Have a look at this, perhaps it doesn't work straight out of the box, but it should give you an idea: