Dhanuka777
Dhanuka777

Reputation: 8636

Loop and update LINQ query returning multiple objects

My LINQ query returns two objects,

 var results = (from r in _context.Obj1
                join ple in _context.Obj2 on r.Obj1ID equals ple.Obj2ID
                where Obj1.Property1 == "something" 
                select new { Obj1 = r, Obj2 = ple}).ToList();

I get two objects into results.

Now I need to loop through and update some attributes of these objects,

something like,

 foreach (var res in results )
            {
                res.Obj1.Property1 = "Completed";
                //Update Obj1
                res.Obj2.Property1 = "Done";
            }

But I can not access Obj1 as res.Obj1 in the foeach loop. How can I do this?

Upvotes: 0

Views: 62

Answers (1)

You should be able to access the properties of the anonymous object within the foreach loop. Do you get any compile or runtime errors?

Alternate solution

Create a POCO for your results:

class MyClass {
    public string Obj1 { get; set; }
    public string Obj2 { get; set; }
}

Use the object:

select new MyClass() { Obj1 = r, Obj2 = ple}

Upvotes: 1

Related Questions