Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于规则的列表合并<;A>;列表中的元素<;B>;在C中#_C#_List_Linq_Automapper_Valueinjecter - Fatal编程技术网

C# 基于规则的列表合并<;A>;列表中的元素<;B>;在C中#

C# 基于规则的列表合并<;A>;列表中的元素<;B>;在C中#,c#,list,linq,automapper,valueinjecter,C#,List,Linq,Automapper,Valueinjecter,我有一张单子。基于A类型对象的一些属性,我想从列表中合并一个或多个A类型对象,并形成一个B类型的新对象。 合并将基于一些预定义的规则 类似于automapper,但输入是一个对象列表 Example class A { Guid source; int ref; string text1; string text2; int Value } class B { List<Guid> source; int ref; List<string> text;

我有一张单子。基于A类型对象的一些属性,我想从列表中合并一个或多个A类型对象,并形成一个B类型的新对象。 合并将基于一些预定义的规则

类似于automapper,但输入是一个对象列表

Example 
class A {
 Guid source;
 int ref;
 string text1;
 string text2;
 int Value
}
class B {
 List<Guid> source;
 int ref;
 List<string> text;
 int Value
}
示例
甲级{
Guid源;
int-ref;
字符串text1;
字符串text2;
整数值
}
B类{
列出来源;
int-ref;
列表文本;
整数值
}
对于具有相同ref的列表中的所有元素,我希望基于如下规则进行合并

如果源中有值(source1、source2、source3)

然后B.text=a1.text1+a2.text1+a3.text1

b.值=a1.值+a2.值+a3.值

但是如果source在(source4)中有值

然后是一个简单的值映射

这是一种配置此类合并规则的简单而有效的方法

提前谢谢


假设列表有100个元素,有30个不同的ref值,对于reach ref值,有多个源值。因此列表中元素的最终计数将小于100。

您可以在LINQ查询中的多列(
ref
source
)上使用
GroupBy
,以执行所需的转换。棘手的部分是在第二列使用条件分组

由于您有一个特殊的
源(例如source4)
,因此不需要对其进行分组。因此,可以为
GroupBy
添加一个虚拟列,如果
source
不是
source4
,而是
new unique(new Guid())
如果其
source4
,则该列将具有固定值

// source4 : No grouping needed on this
var guidUngroup = new Guid("aed48532-0a92-4093-bbb1-19afe64cef15");
var bObjects = aObjects
                .GroupBy(a => new
                {
                    a.refVal,
                    dummySource = a.source == guidUngroup ?Guid.NewGuid():guidUngroup
                })
                .Select(g => new B
                 {
                     source = g.Select(a => a.source).ToList<Guid>(),
                     refVal = g.Key.refVal,
                     text = g.Select(a => a.text1).ToList<string>(),
                     Value = g.Sum(a => a.Value)
                 })
                 .ToList();

在LINQ查询中,您可以对多列(
ref
source
)使用
GroupBy
,以执行所需的转换。棘手的部分是在第二列使用条件分组

由于您有一个特殊的
源(例如source4)
,因此不需要对其进行分组。因此,可以为
GroupBy
添加一个虚拟列,如果
source
不是
source4
,而是
new unique(new Guid())
如果其
source4
,则该列将具有固定值

// source4 : No grouping needed on this
var guidUngroup = new Guid("aed48532-0a92-4093-bbb1-19afe64cef15");
var bObjects = aObjects
                .GroupBy(a => new
                {
                    a.refVal,
                    dummySource = a.source == guidUngroup ?Guid.NewGuid():guidUngroup
                })
                .Select(g => new B
                 {
                     source = g.Select(a => a.source).ToList<Guid>(),
                     refVal = g.Key.refVal,
                     text = g.Select(a => a.text1).ToList<string>(),
                     Value = g.Sum(a => a.Value)
                 })
                 .ToList();

B.text
字符串的
列表
。通过
B.text=a1.text1+a2.text1+a3.text1
,您想要实现什么?研究AutoMapper.Collection@MKR,实际上我有两种情况。
B.text
字符串的
列表
。通过
B.text=a1.text1+a2.text1+a3.text1,您想要实现什么?研究AutoMapper.Collection.@MKR,实际上我有两种方案。