gpt4 book ai didi

c# - 将单个对象转换为多个对象 c#

转载 作者:行者123 更新时间:2023-11-30 23:06:54 25 4
gpt4 key购买 nike

我有一个列表如下

enter image description here

我想将其转换成如下所示的列表

enter image description here

谁能推荐一些示例代码?

我试过下面的代码

class Program
{
static void Main(string[] args)
{
var mockForDataColection = Mock.MockForDataColection();
Factory f = new Factory();
List<ResultSet> rl = new List<ResultSet>();
var groups = mockForDataColection.GroupBy(x => new { group1 = x.GroupIdOne, group2 = x.GroupIdTwo, group3 = x.GroupIdThree }).ToList();

f.Name = mockForDataColection.FirstOrDefault(c => c.Key == "Chamber").Value;
f.RunNumber = mockForDataColection.FirstOrDefault(c => c.Key == "RunNumber").Value;
var groupBy = mockForDataColection.GroupBy(c => c.GroupIdThree).Skip(1);
foreach (var dataCollection in groupBy)
{
ResultSet r = new ResultSet();
r.BarCode = dataCollection.FirstOrDefault(d => d.Key == "BarCode").Value;
r.IsPassed = dataCollection.FirstOrDefault(d => d.Key == "IsPassed").Value;
rl.Add(r);

}
f.ResultSet = rl;
}


}

public class DataCollection
{
public int GroupIdOne { get; set; }
public int GroupIdTwo { get; set; }
public int GroupIdThree { get; set; }
public string Key { get; set; }
public string Value { get; set; }

}

public class Factory
{
public string Name { get; set; }
public string RunNumber { get; set; }
public List<ResultSet> ResultSet { get; set; }


}

public class ResultSet
{
public string BarCode { get; set; }
public string IsPassed { get; set; }
public int FailiureCode { get; set; }
}

public static class Mock
{
public static List<DataCollection> MockForDataColection()
{
List<DataCollection> dataCollectionList = new List<DataCollection>();
DataCollection dataCollection = new DataCollection
{
GroupIdOne = 1,
GroupIdTwo = 1,
GroupIdThree = 0,
Key = "Chamber",
Value = "Test Chamber"
};
DataCollection dataCollection1 = new DataCollection
{
GroupIdOne = 1,
GroupIdTwo = 1,
GroupIdThree = 0,
Key = "RunNumber",
Value = "2"
};

DataCollection dataCollection2 = new DataCollection
{
GroupIdOne = 1,
GroupIdTwo = 2,
GroupIdThree = 3,
Key = "IsPassed",
Value = "P"
};

DataCollection dataCollection3 = new DataCollection
{
GroupIdOne = 1,
GroupIdTwo = 2,
GroupIdThree = 3,
Key = "BarCode",
Value = "PQWERT"
};


DataCollection dataCollection4 = new DataCollection
{
GroupIdOne = 1,
GroupIdTwo = 2,
GroupIdThree = 4,
Key = "IsPassed",
Value = "F"
};

DataCollection dataCollection5 = new DataCollection
{
GroupIdOne = 1,
GroupIdTwo = 2,
GroupIdThree = 4,
Key = "BarCode",
Value = "IUTYY"
};

dataCollectionList.Add(dataCollection);
dataCollectionList.Add(dataCollection1);
dataCollectionList.Add(dataCollection2);
dataCollectionList.Add(dataCollection3);
dataCollectionList.Add(dataCollection4);
dataCollectionList.Add(dataCollection5);

return dataCollectionList;
}
}

我认为这不是一个好的代码。请建议其他一些简单的方法来实现这一目标。

需要将数据采集类转化为工厂类。在数据中,集合类具有键和值属性。键在工厂类中充当属性,值是值。根据 GroupId Only 我们将决定它会转到父类 (Factory) 还是子类 (ResultSet)。

GroupId 一个值相同意味着它都在一个实体下。 GroupId 两个值不同表示是相关实体。

最佳答案

我已经完全编辑了答案,更改了所有内容,使其正确并符合所问的内容。

listA,你要分组的数据

var listA = new List<DataCollection>
{
new DataCollection { GroupIdOne = 1, GroupIdTwo = 1, GroupIdThree = 0, Key = "Chamber", Value = "Test Chamber" },
new DataCollection { GroupIdOne = 1, GroupIdTwo = 1, GroupIdThree = 0, Key = "RunNumber", Value = "2" },
new DataCollection { GroupIdOne = 1, GroupIdTwo = 2, GroupIdThree = 3, Key = "IsPassed", Value = "P" },
new DataCollection { GroupIdOne = 1, GroupIdTwo = 2, GroupIdThree = 3, Key = "BarCode", Value = "PQWERT" },
new DataCollection { GroupIdOne = 1, GroupIdTwo = 2, GroupIdThree = 4, Key = "IsPassed", Value = "F"},
new DataCollection { GroupIdOne = 1, GroupIdTwo = 2, GroupIdThree = 4, Key = "BarCode", Value = "IUTYY"}
};

列表B

var listB = listA
.Where(x => x.Key.Contains("BarCode"))
.GroupBy(g => new {g.GroupIdOne, g.GroupIdTwo, g.GroupIdThree, g.Key, g.Value})
.Select(s =>
new ResultSet
{
BarCode = s.Key.Value,
IsPassed = listA.FirstOrDefault(a => a.Key.Equals("IsPassed")
&& a.GroupIdOne == s.Key.GroupIdOne
&& a.GroupIdTwo == s.Key.GroupIdTwo
&& a.GroupIdThree == s.Key.GroupIdThree

)?.Value ?? ""
})
.ToList();

关于c# - 将单个对象转换为多个对象 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47792405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com