I have two Enumberable lists: list1 & list2 I want to take something from list2 and update it in list1 based on a condition
e.g: list1.Id has say for instance 1, 2, 3, 4, 5 etc. list2.Id has 3, 4 I need to compare these Ids and take the other fields(e.g. name, subject) from list2 which matches list1.Id (3 and 4 in this case) and copy it to list1 other fields(name, subject)
list1:
Id | Name | Subject |
---|---|---|
1 | N1 | S1 |
2 | N2 | S2 |
3 | ||
4 | ||
5 | N5 | S5 |
list2:
Id | Name | Subject |
---|---|---|
3 | N3 | S3 |
4 | N4 | S4 |
Required resultant:
Id | Name | Subject |
---|---|---|
1 | N1 | S1 |
2 | N2 | S2 |
3 | N3 | S3 |
4 | N4 | S4 |
5 | N5 | S5 |
CodePudding user response:
Lets say you have a class subject as below:
class Suabject
{
public int id;
public string name;
public string Subject;
}
and you have two lists as below:
List<Suabject> enum1 = new List<Suabject>();
List<Suabject> enum2 = new List<Suabject>();
If you want to merge them, what you do is basically go through all the items in enum2 and check if the item exist in the enum1 then u update it. If not, you can add it to the list. As below:
foreach (var item in enum2)
{
var s = enum1.FirstOrDefault(x=> x.id == item.id);
if(s != null)
{
s.name = item.name;
s.Subject = item.Subject;
}
else
enum1.Add(s);
}
In this way, enum1 will be the merged list of the two.
CodePudding user response:
How about this?
var listOne = new[]
{
new Entity { Id = 1, Name = "N1", Subject = "S1" },
new Entity { Id = 2, Name = "N2", Subject = "S2" },
new Entity { Id = 3 },
new Entity { Id = 4 },
new Entity { Id = 5, Name = "N5", Subject = "S5" },
};
var listTwo = new[]
{
new Entity { Id = 3, Name = "N3", Subject = "S3" },
new Entity { Id = 4, Name = "N4", Subject = "S4" },
};
// Join to lists by using 1:1 relationship
var pairs = listOne.Join(listTwo, entity => entity.Id, entity => entity.Id, (one, two) => (one, two));
foreach (var pair in pairs)
{
// Update first name, if empty
if(string.IsNullOrEmpty(pair.one.Name))
pair.one.Name = pair.two.Name;
// Update first subject, if empty
if(string.IsNullOrEmpty(pair.one.Subject))
pair.one.Subject = pair.two.Subject;
}
// Save and show elements
foreach (var item in listOne)
{
Console.WriteLine(JsonConvert.SerializeObject(item));
}
CodePudding user response:
If you are looking for a solution without a Foreach, have you thought of using Dictionaries for this part, where you set the key as the id of your object and the value as the object itself.
Dictionary<int, ObjectType> dictA = new Dictionary<int, ObjectType>();
where you add you values like this:
dictA.Add(object.Id, object);
Then you will be able to do a merge without a Foreach like this:
dictA = dictA.Concat(dictB.Where(b => !dictA.ContainsKey(b.Key))).ToDictionary(b => b.Key, b => b.Value);
This way, the dictB gets merged in the dictA. Just reverse the values if you want the opposite to happen.
CodePudding user response:
Not quite sure what do you need. I assume you need to update data in enum1 based on enum2, in that case do this:
foreach(var item in enum2)
{
var itemToUpdate = enum1.SingleOrDefault(x => x.Id == item.Id);
if (itemToUpdate == null) continue;
itemToUpdate.Name = item.Name;
itemToUpdate.Subject = item.Subject;
}