If you want to update an property of an object in LINQ without creating new objects, you can use the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Assert = Xunit.Assert;

namespace Research.Rli.Tests
{
    public class Appointment
    {
        public int Id { get; set; }
        public string Location { get; set; }
    }

    [TestClass]
    public class ResearchTester
    {
        [TestMethod]
        public void Should_update_appointments()
        {
            var appointments1 = new List<Appointment>
            {
                new Appointment { Id = 1, Location = "" },
                new Appointment { Id = 2, Location = "" },
                new Appointment { Id = 3, Location = "" }
            };

            var appointments2 = new List<Appointment>
            {
                new Appointment { Id = 1, Location = "My location 1" },
                new Appointment { Id = 2, Location = "" },
                new Appointment { Id = 3, Location = "My location 3" }
            };

            Func<Appointment, Appointment, Appointment> UpdateLocation 
                = ((a, b) => { a.Location = b.Location; return a; });

            var updatedAppointmets = (from a1 in appointments1
                                     join a2 in appointments2 on a1.Id equals a2.Id
                                      select UpdateLocation(a1, a2)).ToList();

            foreach (Appointment a in updatedAppointmets)
            {
                Console.WriteLine
                (
                    string.Format("Id [{0}] Location [{1}]", a.Id, a.Location)
                );
            }

            // Output:
            // Id [1] Location [My location 1]
            // Id [2] Location []
            // Id [3] Location [My location 3]
        }
    }
}


If you want to use a LEFT OUTER JOIN in the linq query, use:

 

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Assert = Xunit.Assert;

namespace Research.Rli.Tests
{
    public class Appointment
    {
        public int Id { get; set; }
        public string Location { get; set; }
    }

    [TestClass]
    public class ResearchTester
    {
        [TestMethod]
        public void Should_update_appointments()
        {
            var appointments1 = new List<Appointment>
            {
                new Appointment { Id = 1, Location = "" },
                new Appointment { Id = 2, Location = "" },
                new Appointment { Id = 3, Location = "" }
            };

            var appointments2 = new List<Appointment>
            {
                new Appointment { Id = 1, Location = "My location 1" },
                new Appointment { Id = 3, Location = "My location 3" }
            };

            Func<Appointment, Appointment, Appointment> UpdateLocation
                = ((a, b) => { if (b != null) { a.Location = b.Location; } return a; });

            var updatedAppointmets =
            (
                from a1 in appointments1
                join a2 in appointments2 on a1.Id equals a2.Id into g
                from g1 in g.DefaultIfEmpty(null)
                select UpdateLocation(a1, g1)
            ).ToList();

            foreach (Appointment a in updatedAppointmets)
            {
                Console.WriteLine
                (
                    string.Format("Id [{0}] Location [{1}]", a.Id, a.Location)
                );
            }

            // Output:
            // Id [1] Location [My location 1]
            // Id [2] Location []
            // Id [3] Location [My location 3]
        }
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.