Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 list EList and OList. From the union, get the distinct results into var b.

how do i manipulate the result in var b to produce new result that have an extra field to indicate that the particulate row of data is from EList or OList. thanks.

The expected end result (Field arangement) should be something this format:
ID, Name, Email, IS_Elist, IS_Olist
1, a1, b1, True, True
2, a2, b2, True, False
3, aa2, bb2 False, True

XML
List<EmployeeInfo> EList = new List<EmployeeInfo>();
EList.Add(new EmployeeInfo(1, "a1", "b1"));
EList.Add(new EmployeeInfo(2, "a2", "b2"));

List<EmployeeInfo> OList = new List<EmployeeInfo>();
OList.Add(new EmployeeInfo(1, "aa1", "bb1"));
OList.Add(new EmployeeInfo(3, "aa2", "bb2"));

var b = EList.Union(OList)
        .GroupBy(g => new { g.ID, g.Name, g.Email})
        .Select(s => new EmployeeInfo
        {
            ID = s.Key.ID,
            Name = s.Key.Name,
            Email = s.Key.Email
        });
Posted
Updated 17-Dec-12 5:46am
v2

1 solution

Hi,
Try this,
C#
List<employeeinfo> EList = new List<employeeinfo>();
EList.Add(new EmployeeInfo(1, "a1", "b1"));
EList.Add(new EmployeeInfo(2, "a2", "b2"));
 
List<employeeinfo> OList = new List<employeeinfo>();
OList.Add(new EmployeeInfo(1, "aa1", "bb1"));
OList.Add(new EmployeeInfo(3, "aa2", "bb2"));

var result = (
    from e in EList
    select new { 
      Id = e.ID, 
      Name = e.Name,
      Email = e.Email,
      IS_Elist = true, 
      IS_Olist = false }
  ).Union(
    from o in OList 
    select new { 
      Id = o.ID, 
      Name = o.Name, 
      Email = o.Email,
      IS_Elist = false, 
      IS_Olist = true }
  ).ToList();


Here you have to create new list with 5 column and have to put IS_Olist false in first query and IS_Elist false in second query.

I hope this will help.
Thanks :)
 
Share this answer
 
v2
Comments
Tina Ng 17-Dec-12 18:35pm    
the result show duplicate when i have list like below. the solution provided produce 5 rows of results, but expected is three row. Btw, i have some work around, do anyone know why this query don't work. the conditional part keep always return false. any idea?

var results = (
from e in b
select new
{
Id = e.ID,
Name = e.Name,
Email = e.Email,
IS_Elist = (EList.Contains(e))?true:false,
IS_Olist = (OList.Contains(e)) ? true : false,
}
).ToList();

List<employeeinfo> EList = new List<employeeinfo>();
EList.Add(new EmployeeInfo(1, "a1", "b1"));
EList.Add(new EmployeeInfo(2, "a2", "b2"));

List<employeeinfo> OList = new List<employeeinfo>();
OList.Add(new EmployeeInfo(1, "a1", "b1"));
OList.Add(new EmployeeInfo(2, "a2", "b2"));
OList.Add(new EmployeeInfo(3, "aa2", "bb2"));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900