问题描述
我正在尝试进行这样的转换:
ID | 值1 | Value2 | Value3 |
---|---|---|---|
1 | id1val1 | id1val2 | id1val3 |
2 | id2val1 | id2val2 | id2val3 |
1 | id1val1 | id1val2 | id1val4 |
3 | id3val1 | id3val2 | id3val3 |
变成这样:
ID | 值1 | Value2 | Value3 |
---|---|---|---|
1 | id1val1 | id1val2 | id1val3,id2val4 |
2 | id2val1 | id2val2 | id2val3 |
3 | id3val1 | id3val2 | id3val3 |
public RetType{
int id {get; set;}
string Value1 {get; set;}
string Value2 {get; set;}
List<string> Value3 {get; set;}
}
是否有用于获取此结果的 LINQ 方法,因为我有一个由另一个 LINQ 获取的对象,如下所示:
var result = Linq_query...select new{id = tableX.idVal,Value1 = tableY.val1,Value2 = tableX.val2,Value3 = tableZ.val3}
我尝试过这样的事情:
var groupResult = result.GroupBy(r => r.id).Select(grp =>.ToList()).ToList();
但它没有返回我所期望的,它只是将两个 id=1 行分组到一个数组中。 提前致谢!
解决方法
我希望我正确理解了你的意图。现在假设 id 更改时 Value2 不会更改(因为您没有提到如何处理这种情况),您可以执行以下操作,
BOOL AlphaBlend(
HDC hdcDest,int xoriginDest,int yoriginDest,int wDest,int hDest,HDC hdcSrc,int xoriginSrc,int yoriginSrc,int wSrc,int hSrc,BLENDFUNCTION ftn
);
请注意,在您的 AlphaBlend(hdcDest,xoriginDest,yoriginDest,16,skinDc,100,200,blend);
中,您使用了 public class RetType{
public int id {get; set;}
public string Value1 {get; set;}
public string Value2 {get; set;}
public List<string> Value3 {get; set;}
}
var finalResult = result.GroupBy(x=>x.Id)
.Select(x=> new RetType
{
id = x.First().Id,Value2 = x.First().Value2,Value3 = x.Select(c=>c.Value3).ToList()
})
来表示 Value3。如果您需要将其作为逗号分隔的字符串(如 OP 中的表格),您可以使用 RetType
List<string>
其中 RetType 定义为
string.Join