Creating new objects
You can use LINQ/Select to convert the B's into A's and add them with AddRange.
lista.AddRange
(
listb.Select
(
b => new A
{
id = b.id,
val2 = b.val2,
val3 = b.val3
}
)
);
Putting both types of objects in the same list
Or, if you literally want to store both A's and B's in the same list, you can set up an inheritance relationship:
class B
{
public string id { get; set; }
public string val2 { get; set; }
public string val3 { get; set; }
}
class A : B
{
public string val1 { get; set; }
public string val4 { get; set; }
public string val5 { get; set; }
}
var listb = new List<B>();
Now your list can store both A and B, because A is a specialization of B. So you can do this:
listb.AddRange(lista);
Updating in place
If you want to keep the original objects in lista and just want to update the matching properties, you should probably use a loop. It's possible to do it with LINQ if you really want, but it's really not "meant" for updating things in place.
It does make things a little easier to store your As in a dictionary, since you get the lookup ability. A dictionary uses a hash table internally so you will also get better performance than a scan. Because the dictionary only contains references, the objects in the original list will get updated too.
var dictionarya = lista.ToDictionary
(
item => item.id,
item => item
);
foreach (var b in listb)
{
A a;
if (dictionarya.TryGetValue(b.id, out a))
{
a.val2 = b.val2;
a.val3 = b.val3;
}
}
new A()and copy properties fromB, and add this new object instead ofB.