Home > Mobile >  Check if value from one list is present in specific ID of another list
Check if value from one list is present in specific ID of another list

Time:12-10

I have 2 object lists.

  1. The first list has:
ID NAME
1 CAT
2 DOG
  1. The second list has the below details:
Name OBJ ID
CAT 500
DOG 500
CAT 600
DOG 600
DOG 700

Note: I have a total of 3 OBJ IDS 500,600,700 in list 2

How can I find that CAT from list 2 is missing a value of 700 in it? Output should only show CAT is missing for 700. As DOG is having all it's values 500,600,700 but CAT is only having 500,600 not 700

List<Employee> e1 = new List<Employee>();
e1.Add(new Employee { Name = "CAT" });
e1.Add(new Employee { Name = "DOG" });

List<company> c1 = new List<company>();
c1.Add(new company { objid="1", cname = "CAT", id = "500" });
c1.Add(new company { objid = "2", cname = "DOG", id = "500"});
c1.Add(new company { objid = "3", cname = "CAT", id = "600" });
c1.Add(new company { objid = "4", cname = "DOG", id = "600" });
c1.Add(new company { objid = "5", cname = "DOG", id = "700" });

var QSOuterJoin = from emp in  e1
                  join add in c1
                  on emp.Name equals add.cname
                  into EmployeeNameGroup
                  from address in EmployeeNameGroup.DefaultIfEmpty()
                  select new  
                        { emp,
                          address };

List<int> uniqlist = new List<int>();
uniqlist.Add(500);
uniqlist.Add(600);
uniqlist.Add(700);

foreach (var i in QSOuterJoin)
{
  foreach (var j in uniqlist)
  {
    if (String.IsNullOrEmpty(i.address?.id))
    {
      Console.WriteLine("{0} is not {1} {2}", i.emp.Name, i.address?.id, j);
    }
  }
}

CodePudding user response:

var ids = c1.Select(x => x.id).Distinct().ToList();
    var result = c1.GroupBy(x => x.cname)
                   .SelectMany(g => ids.Except(g.Select(y => (y.id)))
                                    .Select(x => (Name: g.Key, Id: x)));
  • Related