Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Linq join update without creating new

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]
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# razor @html.actionlink( edit bootstrap 
Csharp :: Delete last modification on EntityFramework Core 
Csharp :: struct 
Csharp :: Runtime.getRuntime().addShutdownHook(printingHook); c# 
Csharp :: EF will not create columns RULE 
Csharp :: fixed angle unity 
Csharp :: C# Printing Variables and Literals using WriteLine() and Write() 
Csharp :: dynamic c# .add 
Csharp :: c# linq get one object 
Csharp :: netlifycms disable preview 
Csharp :: obs mfplat.dll 
Csharp :: c# get digits from int 
Csharp :: copy properties from two subclasses c# 
Csharp :: Nested objects with linq expression 
Csharp :: Area Of the triangle with condition 
Csharp :: how to make infinite loop in c# 
Csharp :: get position of gameobject unity 
Csharp :: check list exist in list c# if matches any 
Csharp :: unity destroy 
Csharp :: c# if int is even 
Csharp :: run as administrator vs 2019 
Csharp :: c# lerp 
Csharp :: come controllare se textbox è vuota c# 
Html :: html grundgerüst 
Html :: html5 template 
Html :: html new tab 
Html :: Add Random Image from Web in html 
Html :: how to change the preview image of a website 
Html :: bootstrap two buttons side by side with space 
Html :: html email links 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =