将单个对象转换为多个对象c#
我有一个清单如下 我想把它转换成如下的列表 有人能推荐一些示例代码吗 我试过下面的代码将单个对象转换为多个对象c#,c#,.net,entity-framework,linq,C#,.net,Entity Framework,Linq,我有一个清单如下 我想把它转换成如下的列表 有人能推荐一些示例代码吗 我试过下面的代码 class Program { static void Main(string[] args) { var mockForDataColection = Mock.MockForDataColection(); Factory f = new Factory(); List<ResultSet> rl = new List<
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;
}
}
类程序
{
静态void Main(字符串[]参数)
{
var mockfordatacollection=Mock.mockfordatacollection();
工厂f=新工厂();
List rl=新列表();
var groups=mockfordatacollection.GroupBy(x=>new{group1=x.GroupIdOne,group2=x.GroupIdTwo,group3=x.GroupIdThree});
f、 Name=mockfordatacollection.FirstOrDefault(c=>c.Key==“Chamber”).Value;
f、 RunNumber=mockfordatacollection.FirstOrDefault(c=>c.Key==“RunNumber”).Value;
var groupBy=mockfordatacollection.groupBy(c=>c.GroupIdThree).Skip(1);
foreach(groupBy中的var数据收集)
{
结果集r=新结果集();
r、 条形码=数据收集.FirstOrDefault(d=>d.Key==“条形码”).Value;
r、 IsPassed=dataCollection.FirstOrDefault(d=>d.Key==“IsPassed”).Value;
rl.添加(r);
}
f、 结果集=rl;
}
}
公共类数据收集
{
public int GroupIdOne{get;set;}
public int GroupIdTwo{get;set;}
public int GroupIdThree{get;set;}
公共字符串密钥{get;set;}
公共字符串值{get;set;}
}
公营工厂
{
公共字符串名称{get;set;}
公共字符串RunNumber{get;set;}
公共列表结果集{get;set;}
}
公共类结果集
{
公共字符串条形码{get;set;}
公共字符串IsPassed{get;set;}
public int FailiureCode{get;set;}
}
公共静态类模拟
{
公共静态列表mockfordatacollection()
{
List dataCollectionList=新列表();
DataCollection DataCollection=新数据收集
{
GroupIdOne=1,
GroupIdTwo=1,
GroupIdThree=0,
Key=“Chamber”,
Value=“试验箱”
};
DataCollection dataCollection1=新数据收集
{
GroupIdOne=1,
GroupIdTwo=1,
GroupIdThree=0,
Key=“RunNumber”,
Value=“2”
};
DataCollection dataCollection2=新数据收集
{
GroupIdOne=1,
GroupIdTwo=2,
组IDThree=3,
Key=“IsPassed”,
Value=“P”
};
DataCollection dataCollection3=新数据收集
{
GroupIdOne=1,
GroupIdTwo=2,
组IDThree=3,
Key=“条形码”,
Value=“PQWERT”
};
DataCollection dataCollection4=新数据收集
{
GroupIdOne=1,
GroupIdTwo=2,
组IDThree=4,
Key=“IsPassed”,
Value=“F”
};
DataCollection dataCollection5=新数据收集
{
GroupIdOne=1,
GroupIdTwo=2,
组IDThree=4,
Key=“条形码”,
Value=“IUTYY”
};
dataCollectionList.Add(dataCollection);
dataCollectionList.Add(dataCollection1);
dataCollectionList.Add(dataCollection2);
dataCollectionList.Add(dataCollection3);
dataCollectionList.Add(dataCollection4);
dataCollectionList.Add(dataCollection5);
返回数据收集列表;
}
}
我认为这不是一个好代码。请提出一些其他简单的方法来实现这一点
需要将数据收集类转换为Factory类。在数据中,集合类具有键和值属性。键在Factory类中充当属性值为Value。仅基于GroupId,我们将决定它将转到父类(工厂)或子类(结果集)
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"}
};
在A类中,条形码和状态都是键,可能不同。请检查我的数据收集Class@Niventh现在看我的答案,我已经重写了
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();