C#如何合并两个用户定义类型中包含重复项的字典
您没有很好地指定合并C#如何合并两个用户定义类型中包含重复项的字典,c#,linq,list,dictionary,merge,C#,Linq,List,Dictionary,Merge,您没有很好地指定合并 是否要将两个DataContainers列表合并为一个DataContainers列表,其中包含原始to列表中的所有DataContainers 还是要将所有数据容器的所有bockedData合并到一个字典中?当然,只有在所有数据容器中的所有bockedData的键都是唯一的情况下,这才有效 第一个很简单: var gap = fromGap.SelectMany(o => o.BlockedData).ToDictionary(o => o.Key, o
- 是否要将两个
列表合并为一个DataContainers
列表,其中包含原始to列表中的所有DataContainers
DataContainers
- 还是要将所有
数据容器的所有
合并到一个字典中?当然,只有在所有bockedData
中的所有数据容器
的键都是唯一的情况下,这才有效bockedData
var gap = fromGap.SelectMany(o => o.BlockedData).ToDictionary(o => o.Key, o => o.Value);
var orginal = fromFirst.SelectMany(o => o.BlockedData).ToDictionary(o => o.Key, o => o.Value);
var final = orginal.Concat(gap.Where(kvp => !orginal.ContainsKey(kvp.Key))).ToDictionary(x => x.Key, x => x.Value);
或者在一个linq语句中:
IEnumerable<Dictionary<int, byte[]>> allDictionaries = originalReq
.Concat(gapReq)
.Select(datacontainer => datacontainer.bockedData);
IEnumerable<KeyValuePair<int, byte[]>> allBockedDataRecords = allDictionaries
.SelectMany(dictionary => dictionary.Cast<IEnumerable<KeyValuePair<int, byte[]>>);
Dictionary<int, byte[]> result = allBockedDataRecords.ToDictionary(
record => record.Key, // from every record take the Key as dictionary key
record => record.Value, // from every record take value as dictionary value
Dictionary result=originalReq.Concat(gapReq)
.SelectMany(dataContainer=>dataContainer.bockedData)
.ToDictionary(
record=>record.Key,
record=>record.Value);
您想用重复的钥匙做什么?您希望保留哪一个?忽略重复项并获取DataContainer的列表。这就是我的目标。我不需要省略orginalReq中的任何日期,但需要获取gapReq中的新数据。因此,您有两个数据容器列表,您需要执行一些您称为“合并”的操作。是否要将这两个列表合并为一个包含所有数据容器的列表?或者您想将所有数据容器的所有bockedData合并到一个字典中吗?@HaraldCopoolse假设DataContainers BlockedData没有任何重复的键,则这两种方法都可以。或者合并所有数据容器的所有bockedData。我希望这能起作用:fromFirst.AddRange(fromGap.Where(kvp=>!fromFirst.Contains(kvp));
static void Main(string[] args)
{
List<DataContainer> fromFist = new List<DataContainer>();
fromFist = GetFirstreq();
List<DataContainer> fromGap = new List<DataContainer>();
fromGap = GetGap();
//To Do Merge fromFist and fromGap with out dups
}
static List <DataContainer> GetFirstreq()
{
List<DataContainer> firstReq = new List<DataContainer>();
Dictionary<int, byte[]> dict1 = new Dictionary<int, byte[]>();
dict1.Add(1, new byte[] { 0x00, 0x01 });
dict1.Add(2, new byte[] { 0x00, 0x01 });
dict1.Add(3, new byte[] { 0x00, 0x01 });
Dictionary<int, byte[]> dict2 = new Dictionary<int, byte[]>();
dict2.Add(7, new byte[] { 0x00, 0x01 });
dict2.Add(8, new byte[] { 0x00, 0x01 });
dict2.Add(9, new byte[] { 0x00, 0x01 });
firstReq.Add(new DataContainer() { BlockedData = dict1 });
firstReq.Add(new DataContainer() { BlockedData = dict2 });
return firstReq;
}
static List<DataContainer> GetGap()
{
//this can have dups
List<DataContainer> firstReq = new List<DataContainer>();
Dictionary<int, byte[]> dict1 = new Dictionary<int, byte[]>();
dict1.Add(1, new byte[] { 0x00, 0x01 });
dict1.Add(2, new byte[] { 0x00, 0x01 });
dict1.Add(3, new byte[] { 0x00, 0x01 });
//nedded data
Dictionary<int, byte[]> dict2 = new Dictionary<int, byte[]>();
dict2.Add(4, new byte[] { 0x00, 0x01 });
dict2.Add(5, new byte[] { 0x00, 0x01 });
dict2.Add(6, new byte[] { 0x00, 0x01 });
firstReq.Add(new DataContainer() { BlockedData = dict1 });
firstReq.Add(new DataContainer() { BlockedData = dict2 });
return firstReq;
}
}
class DataContainer
{
public Dictionary<int, byte[]> BlockedData { get; set; }
}
List<DataContainer> firstReq = new List<DataContainer>();
Dictionary<int, byte[]> dict1 = new Dictionary<int, byte[]>();
dict1.Add(1, new byte[] { 0x00, 0x01 });
dict1.Add(2, new byte[] { 0x00, 0x01 });
dict1.Add(3, new byte[] { 0x00, 0x01 });
Dictionary<int, byte[]> dict2 = new Dictionary<int, byte[]>();
dict2.Add(4, new byte[] { 0x00, 0x01 });
dict2.Add(5, new byte[] { 0x00, 0x01 });
dict2.Add(6, new byte[] { 0x00, 0x01 });
Dictionary<int, byte[]> dict3 = new Dictionary<int, byte[]>();
dict2.Add(7, new byte[] { 0x00, 0x01 });
dict2.Add(8, new byte[] { 0x00, 0x01 });
dict2.Add(9, new byte[] { 0x00, 0x01 });
firstReq.Add(new DataContainer() { BlockedData = dict1 });
firstReq.Add(new DataContainer() { BlockedData = dict2 });
firstReq.Add(new DataContainer() { BlockedData = dict3 });
var gap = fromGap.SelectMany(o => o.BlockedData).ToDictionary(o => o.Key, o => o.Value);
var orginal = fromFirst.SelectMany(o => o.BlockedData).ToDictionary(o => o.Key, o => o.Value);
var final = orginal.Concat(gap.Where(kvp => !orginal.ContainsKey(kvp.Key))).ToDictionary(x => x.Key, x => x.Value);
IEnumerable<DataContainer> result = originalReq.Concat(gapReq);
List<DataContainer> result = originalReq.Concat(gapReq).ToList();
IEnumerable<Dictionary<int, byte[]>> allDictionaries = originalReq
.Concat(gapReq)
.Select(datacontainer => datacontainer.bockedData);
IEnumerable<KeyValuePair<int, byte[]>> allBockedDataRecords = allDictionaries
.SelectMany(dictionary => dictionary.Cast<IEnumerable<KeyValuePair<int, byte[]>>);
Dictionary<int, byte[]> result = allBockedDataRecords.ToDictionary(
record => record.Key, // from every record take the Key as dictionary key
record => record.Value, // from every record take value as dictionary value
Dictionary<int, byte[]> result = originalReq.Concat(gapReq)
.SelectMany(dataContainer => dataContainer.bockedData)
.ToDictionary(
record => record.Key,
record => record.Value);