我有一个列表如下
我想将其转换为一个看起来像这样的列表
有人可以提供一些示例代码吗?
我试过了下面的代码
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;
}
}
我认为这不是一个好的代码。请建议一些其他简单的方法来实现这一目标。
需要将数据集合类转换为Factory类。在数据中,集合类具有键和值属性。 Key在Factory类中充当属性Value is Value。基于GroupId,我们将决定它将转到Parent类(Factory)或子类(ResultSet)。
GroupId一个值相同意味着它在一个实体下。 GroupId两个值不同意味着它是一个相关实体。
答案 0 :(得分:2)
我已经完全编辑了答案,根据问题改变了所有内容以使其正确无误。
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"}
};
<强>数组listB 强>
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();