Click here to Skip to main content
12,700,567 members (32,799 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# LINQ
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

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 17-Dec-12 6:44am
YeeMun625
Updated 17-Dec-12 6:46am
v2

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,
Try this,
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 :)
  Permalink  
v2
Comments
YeeMun 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170118.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100