I have two lists. One of them has an Id
var a = new List<MyModel>
{
new MyModel()
{
id = 1,
prop1 = 1,
prop2 = 1
},
new MyModel()
{
id = 2,
prop1 = 2,
prop2 = 3
},
};
and another one hasn't, but they are identical on combination of prop1
and prop2
var b = new List<MyModel>
{
new MyModel()
{
prop1 = 1,
prop2 = 1
},
new MyModel()
{
prop1 = 2,
prop2 = 3
},
};
I need to populate list b
with ids from list a
when a.prop1 = b.prop1 && a.prop2 == b.prop2
but I don't know how to write this linq properly
The two list are always identical, but unordered. I think I can sort them and just assign them one by one, but I wonder if there another way
CodePudding user response:
This works for me:
var query =
from bb in b
join aa in a on new { bb.prop1, bb.prop2 } equals new { aa.prop1, aa.prop2 }
select new { bb, aa.id };
foreach (var q in query)
q.bb.id = q.id;