您可以在 LINQ 查询中对多列(在本例中为 ref 和 source)使用 GroupBy 来执行所需的转换。棘手的部分是在第二列使用条件分组。
因为你有一个特殊的source(e.g. source4) 不需要分组。因此,可以添加一个用于GroupBy 的虚拟列,如果source 不是source4,但如果它的source4 是new unique (new Guid()),它将具有固定值。
// 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();
上面code-snippet中用到的类和样本数据是:
// Sample data and result.
public class A
{
public Guid source { get; set; }
public int refVal { get; set; } //Name changed from original post
public string text1 { get; set; }
public string text2 { get; set; }
public int Value { get; set; }
}
public class B
{
public List<Guid> source { get; set; }
public int refVal { get; set; } //Name changed from original post
public List<string> text { get; set; }
public int Value { get; set; }
}
var aObjects = new List<A>{new A(){source = new Guid("c3a52882-069a-4fe5-b37f-0ffe00b3299c"),
refVal = 100,
text1 = "1text1",
text2 = "1text2",
Value = 10 },
new A(){source = new Guid("bcf4cbbf-e01f-48fb-9a45-cc4d9bd5647d"),
refVal = 100,
text1 = "2text1",
text2 = "2text2",
Value = 20 },
new A(){source = new Guid("9f50a0af-3507-4a2b-be25-842f01372194"),
refVal = 100,
text1 = "3text1",
text2 = "3text2",
Value = 30 },
new A(){source = new Guid("aed48532-0a92-4093-bbb1-19afe64cef15"),
refVal = 101,
text1 = "4text1",
text2 = "4text2",
Value = 40 },
new A(){source = new Guid("aed48532-0a92-4093-bbb1-19afe64cef15"),
refVal = 101,
text1 = "5text1",
text2 = "5text2",
Value = 50 }};
//Result : list of B objects stored in aObjects
Source : c3a52882-069a-4fe5-b37f-0ffe00b3299c,bcf4cbbf-e01f-48fb-9a45-cc4d9bd5647d,9f50a0af-3507-4a2b-be25-842f01372194
refVal : 100
Source : 1text1,2text1,3text1
Value : 60
Source : aed48532-0a92-4093-bbb1-19afe64cef15
refVal : 101
Source : 4text1
Value : 40
Source : aed48532-0a92-4093-bbb1-19afe64cef15
refVal : 101
Source : 5text1
Value : 50