将聚合的各个部分映射到 List

发布于 2024-12-10 05:04:25 字数 594 浏览 0 评论 0原文

假设我们有一个简单的业务对象:

class SimpleBO
{
   public string Field1{get;set;}
   public string Field2{get;set;}
}

此外,我们还有一个复杂的聚合,如下所示:

class ComplexBO
{
    public SimpleBO SimpleBOField {get;set}
    public List<SomeClass> ObjectList {get;set;}
    public SomeClass Object {get;set;}
}

SomeClass 本身具有 SimpleBO 的引用:

class SomeClass 
{
    public SimpleBO SimpleBOField {get;set}
}

现在,在我的程序的某些部分,我想获取所有不同的列表简单的对象在某个集合中相遇。我们大量使用自动映射器,但到目前为止我还没有成功映射它。也许 LINQ 查询是更好的选择?你会如何解决这个问题?

Let us say we have a simple business object:

class SimpleBO
{
   public string Field1{get;set;}
   public string Field2{get;set;}
}

Also we have a complex Aggregate like that:

class ComplexBO
{
    public SimpleBO SimpleBOField {get;set}
    public List<SomeClass> ObjectList {get;set;}
    public SomeClass Object {get;set;}
}

SomeClass itself has a reference of SimpleBO:

class SomeClass 
{
    public SimpleBO SimpleBOField {get;set}
}

Now in some part of my program I want to get a list of all distinct simple objects met inside a certain aggreggate. We are using automapper heavily but I did not manage to map it so far. May be a LINQ query is a better option? How would you solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ぺ禁宫浮华殁 2024-12-17 05:04:25

假设您拥有的是:

ComplexBO aggregate = ...

那么您应该只需要:

var objects = aggregate.ObjectList.Select(x => x.SimpleBOField).Concat(
         new[] {aggregate.SimpleBOField, aggregate.Object.SimpleBOField }
    ).Distinct().ToList();

这将为您提供不同的对象引用;如果您需要不同的值对,则可以重写Equals()/GetHashCode(),或者作弊:

var objects = aggregate.ObjectList.Select(x => x.SimpleBOField).Concat(
         new[] {aggregate.SimpleBOField, aggregate.Object.SimpleBOField }
    ).Select(
         x => new {x.SimpleBOField.Field1, x.SimpleBOField.Field2}
    ).Distinct().Select(
         x => new SimpleBO {Field1 = x.Field1, Field2 = x.Field2}
    ).ToList();

Assuming what you have is:

ComplexBO aggregate = ...

then you should just need:

var objects = aggregate.ObjectList.Select(x => x.SimpleBOField).Concat(
         new[] {aggregate.SimpleBOField, aggregate.Object.SimpleBOField }
    ).Distinct().ToList();

This will give you the distinct object references; if you need distinct value pairs, then either override Equals()/GetHashCode(), or cheat:

var objects = aggregate.ObjectList.Select(x => x.SimpleBOField).Concat(
         new[] {aggregate.SimpleBOField, aggregate.Object.SimpleBOField }
    ).Select(
         x => new {x.SimpleBOField.Field1, x.SimpleBOField.Field2}
    ).Distinct().Select(
         x => new SimpleBO {Field1 = x.Field1, Field2 = x.Field2}
    ).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文