-1
Hello, I have the following situation:
public class Foo {
int id { get; set; }
Bar bar { get; set; }
}
public class Bar {
int id { get; set; }
int FooId { get; set; }
}
public class Program {
public static void Main(string[] args){
var foos = new List<Foo> {
new Foo{ id: 1 }, new Foo{ id: 2 },
new Foo{ id: 3 }, new Foo{ id: 4 }
};
var bars = new List<Bar> {
new Bar{ id: 5, FooId: 2},
new Bar{ id: 6, FooId: 4}
}
}
}
So I can insert the bars
within their respective foos
i have to use a foreach to check if the items have property FooId
same as Foo id on the list, so:
foreach(var foo in foos)
foreach(var bar in bars)
if(bar.FooId == foo.Id)
foo.Bar = bar;
If I were to do it in Linq, I would have to generate a new object and assign all the properties of the old one to the new one and then correlate, like this:
foos = foos
.Join(bars,foo => foo.Id,bar => bar.FooId, (foo, bar) => new { foo, bar })
.Select(x=> new Foo{ Id = x.foo.Id, Bar = x.bar })
.ToList();
Is there any way I can harness the object of foos
without the need to recreate it? The idea of this question is because I can do something similar in javascript like this:
var foos = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
var bars = [{id: 5, fooId: 2}, {id: 6, fooId: 4}];
foos = foos.map(x=> x.bar = bars.filter(y => y.fooId === x.id)[0])
what I need is to return all items, regardless of whether they are null or not
– LeandroLuk
@Leandroluk
foos.ForEach(f => f.bar = bars.FirstOrDefault(b => b.FooId == f.id));
does not meet your need? I added another example, but is not as efficient if you seek efficiency your solution withforeach
or withfor
will be the best.– vik
I thought I would have some better way to use without foreach. After all Linq is very efficient in many situations...
– LeandroLuk