LINQ Update List using JOIN with AND Condition
There is a direct way to update a collection list by joining with another but that is based on only one column and which will be ultimate thing to JOIN on. But how we can update if we need to compare more than one column ? We can join using AND query for which we need to join with first column and then where clause with second column to update. Below is detailed example for JOIN and Update based on 2 columns. Demo of same is given below.
Classes definition
Adding data to classes
Here we need to update first collection based on JOIN with 2 columns i.e. strPosition and strRole and finally strGrade values will be updated. The Solution...
Assuming this as a Web API code, Output in Browser will be,
There is a direct way to update a collection list by joining with another but that is based on only one column and which will be ultimate thing to JOIN on. But how we can update if we need to compare more than one column ? We can join using AND query for which we need to join with first column and then where clause with second column to update. Below is detailed example for JOIN and Update based on 2 columns. Demo of same is given below.
Classes definition
public class clsPlayers { public int intId { get; set; } = 0; public string strPlayerName { get; set; } = ""; public string strPosition { get; set; } = ""; public string strRole { get; set; } = ""; public string strGrade { get; set; } = ""; } public class clsPositions { public string strGrade { get; set; } = ""; public string strPosition { get; set; } = ""; public string strRole { get; set; } = ""; }
Adding data to classes
var objclsPlayers = new List<Models.clsPlayers>(); objclsPlayers.Add(new Models.clsPlayers { intId = 1, strGrade = "E", strRole = "Bat", strPlayerName = "Virat", strPosition = "One Down" }); objclsPlayers.Add(new Models.clsPlayers { intId = 2, strGrade = "E", strRole = "Bat", strPlayerName = "Rohit", strPosition = "Opening" }); objclsPlayers.Add(new Models.clsPlayers { intId = 3, strGrade = "E", strRole = "Bat", strPlayerName = "Rahul", strPosition = "Opening" }); objclsPlayers.Add(new Models.clsPlayers { intId = 4, strGrade = "E", strRole = "Ball", strPlayerName = "Kuldip", strPosition = "Spinner" }); objclsPlayers.Add(new Models.clsPlayers { intId = 5, strGrade = "E", strRole = "Ball", strPlayerName = "Kumar", strPosition = "Fast" }); var objclsPositions = new List<Models.clsPositions>(); objclsPositions.Add(new Models.clsPositions { strGrade = "A", strRole = "Bat", strPosition = "One Down" }); objclsPositions.Add(new Models.clsPositions { strGrade = "B", strRole = "Bat", strPosition = "Opening" }); objclsPositions.Add(new Models.clsPositions { strGrade = "C", strRole = "Ball", strPosition = "Spinner" }); objclsPositions.Add(new Models.clsPositions { strGrade = "D", strRole = "Ball", strPosition = "Fast" });
Here we need to update first collection based on JOIN with 2 columns i.e. strPosition and strRole and finally strGrade values will be updated. The Solution...
foreach (var NewData in objclsPlayers.Join(objclsPositions, Players => Players.strPosition, Positions => Positions.strPosition, (Players, Positions) => new { Players, Positions }) .Where(combine => { return combine.Players.strRole == combine.Positions.strRole; })) { NewData.Players.strGrade = NewData.Positions.strGrade; }
Assuming this as a Web API code, Output in Browser will be,
<ArrayOfclsPlayers...> <clsPlayers> <strGrade>A</strGrade> <intId>1</intId>
<strPlayerName>Virat</strPlayerName> <strPosition>One Down</strPosition> <strRole>Bat</strRole> </clsPlayers> <clsPlayers> <strGrade>B</strGrade> <intId>2</intId>
<strPlayerName>Rohit</strPlayerName> <strPosition>Opening</strPosition> <strRole>Bat</strRole> </clsPlayers> <clsPlayers> <strGrade>B</strGrade> <intId>3</intId>
<strPlayerName>Rahul</strPlayerName> <strPosition>Opening</strPosition> <strRole>Bat</strRole> </clsPlayers> <clsPlayers> <strGrade>C</strGrade> <intId>4</intId>
<strPlayerName>Kuldip</strPlayerName> <strPosition>Spinner</strPosition> <strRole>Ball</strRole> </clsPlayers> <clsPlayers> <strGrade>D</strGrade> <intId>5</intId>
<strPlayerName>Kumar</strPlayerName> <strPosition>Fast</strPosition> <strRole>Ball</strRole> </clsPlayers> </ArrayOfclsPlayers>