Hi rani,
Check this example. Now please take its reference and correct your code.
Namespaces
using System.Linq;
using Microsoft.AspNetCore.Mvc.Rendering;
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
List<SelectListItem> list1 = PopulateFruit1();
List<SelectListItem> list2 = PopulateFruit2();
List<SelectListItem> result = new List<SelectListItem>();
result.AddRange(list1.Where(l1 => !list2.Any(l2 => l1.Text == l2.Text)));
result.AddRange(list2.Where(l2 => !list1.Any(l1 => l2.Text == l1.Text)));
ViewBag.Fruits1 = list1;
ViewBag.Fruits2 = list2;
ViewBag.Fruits3 = result;
return View();
}
private static List<SelectListItem> PopulateFruit1()
{
List<SelectListItem> fruits = new List<SelectListItem>();
fruits.Add(new SelectListItem { Value = "Mango", Text = "Mango" });
fruits.Add(new SelectListItem { Value = "Apple", Text = "Apple" });
fruits.Add(new SelectListItem { Value = "Banana", Text = "Banana" });
return fruits;
}
private static List<SelectListItem> PopulateFruit2()
{
List<SelectListItem> fruits = new List<SelectListItem>();
fruits.Add(new SelectListItem { Value = "Orange", Text = "Orange" });
fruits.Add(new SelectListItem { Value = "Mango", Text = "Mango" });
fruits.Add(new SelectListItem { Value = "Guava", Text = "Guava" });
fruits.Add(new SelectListItem { Value = "Grapes", Text = "Grapes" });
return fruits;
}
}
View
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<span>Fruit 1: </span>
<select asp-items="@ViewBag.Fruits1">
<option value="0">Please select</option>
</select>
<span>Fruit 2: </span>
<select asp-items="@ViewBag.Fruits2">
<option value="0">Please select</option>
</select>
<span>Fruit 3: </span>
<select asp-items="@ViewBag.Fruits3">
<option value="0">Please select</option>
</select>
</body>
</html>
Screenshot