I'm trying to test just how far LINQ can really go. What i'm trying to achieve is property assignments on a list of objects with a single expression rather than for loops. I want to take all items in listA and update the IsMatched
property, but only where there is a corresponding item in listB (which is a different type), is this possible?
Sample Code:
public struct A { public int x; public bool IsMatched;}
public struct B {public int x;}
static void Main(string[] args)
{
List<A> listA = new List<A>();
List<B> listb = new List<B>();
listA.Add(new A() { x=1});
listA.Add(new A() { x=2});
listA.Add(new A() { x=3});
listb.Add(new B() { x=2});
listb.Add(new B() { x=3});
listA = listA.SelectMany(fb => listb, (fb, j) => new {a=fb, b=j})
.Where (anon => anon.b.x == anon.a.x).Select(anon => new A() {x=anon.a.x, IsMatched=true})
.ToList(); // this does not do what I want.
}
I've tried using SelectMany but this only returns the items that matched, or a Cartesian product which I don't want.