组合 2 个 IQueryable 结果

发布于 2024-10-16 09:14:06 字数 822 浏览 2 评论 0原文

我发现了一些看起来相似但不完全相同的问题,所以我会继续回答。

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

我三岁 2024-10-23 09:14:06

看看这个,也许它不能直接开箱即用,但它应该给你一个想法:

public class EntityA : IEntity
{}

public class EntityB : IEntity
{}

List<IEntity> results = 
(from a in EntityAList select a).Cast<IEntity>()
.Concat(
(from b in EntityBList select b).Cast<IEntity>()
)
.Fetch(N).ToList();

foreach (IEntity entity in results)
{
 if (entity is EntityA)
  // do something with entity A

 if (entity is EntityB)
  // do something with entity B
}

Have a look at this, perhaps it doesn't work straight out of the box, but it should give you an idea:

public class EntityA : IEntity
{}

public class EntityB : IEntity
{}

List<IEntity> results = 
(from a in EntityAList select a).Cast<IEntity>()
.Concat(
(from b in EntityBList select b).Cast<IEntity>()
)
.Fetch(N).ToList();

foreach (IEntity entity in results)
{
 if (entity is EntityA)
  // do something with entity A

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