I have 2 lists: list and listLookup
How do I update all the ValueToGet in list with closes KeyLookup in listLookup?
https://dotnetfiddle.net/QHd0Rr
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//Arrange
//Keys needed to get ValueToGet property
var key1 = new WidgetA{Id = 1, Key = 52};
var key2 = new WidgetA{Id = 2, Key = 102};
var key3 = new WidgetA{Id = 3, Key = 152};
List<WidgetA> list = new List<WidgetA>();
list.Add(key1);
list.Add(key2);
list.Add(key3);
//Lookups
var keyLookup1 = new WidgetB()
{Id = 1, KeyLookup = 50, ValueLookup = "Fifty"};
var keyLookup2 = new WidgetB()
{Id = 2, KeyLookup = 100, ValueLookup = "One hundred"};
var keyLookup3 = new WidgetB()
{Id = 3, KeyLookup = 150, ValueLookup = "One hundred and fifty"};
List<WidgetB> listLookup = new List<WidgetB>();
listLookup.Add(keyLookup1);
listLookup.Add(keyLookup2);
listLookup.Add(keyLookup3);
//Act
/* Update all rows for ValueToGet property in list, using the closes KeyLookup in listLookup
Expected result:
key1: Key = 52, ValueToGet = "Fifty"
key2: Key = 102, ValueToGet = “One hundred”
key3: Key = 152, ValueToGet = “One hundred and fifty”
*/
}
}
public class WidgetA
{
public int Id { get; set; }
public int Key { get; set; }
public string ValueToGet { get; set; }
}
public class WidgetB
{
public int Id { get; set; }
public int KeyLookup { get; set; }
public string ValueLookup { get; set; }
}
In SQL it would be kind of like this but finding the closest key somehow:
update list
set ValueToGet = ValueLookup
from list l1
join listLookup l2
on l1.key = l2.keyLookup