Help with two list<>'s to compare differences

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 3304 Views - Last Post: 04 March 2011 - 11:59 AM Rate Topic: -----

#1 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Help with two list<>'s to compare differences

Posted 04 March 2011 - 07:09 AM

Hello All,

I new to blogging and I have recently got use to C# but I'm stuck I have two generic lists first been Users and secound been FileUsers.

private List<users> Users = new List<users>();
private List<FileUsers> Personal = new List<FileUsers>();



the "Users" list has information such as Firstname, Surname, Email, LeftCompany all for one user,
the "FileUser" has the same infromation.

I need to compaire the "FileUsers" agenst the "Users"(this is from a database) and then any user's that are different in to a new list<Different>UsersDifferent = new list<Different>.

problem been that the "Users" list can be 5000 records long and the "FileUser" can be 50 long and if a new user is in the "Fileuser" then it gets added to the "UsersDifferent" list.

I Hop you get what I'm trying to do.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Help with two list<>'s to compare differences

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 07:15 AM

Have you looked into the .Intersect method?

For example, this gets all countries which both employees and customers live.
var infoQuery =
    (from cust in db.Customers
    select cust.Country)
    .Intersect
        (from emp in db.Employees
        select emp.Country)
;


Here's a much more useful example for you. :)
http://msdn.microsof...v=VS.90%29.aspx

Happy coding.

This post has been edited by Sergio Tapia: 04 March 2011 - 07:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 07:30 AM

I cant Seem To get that to work ill upload my code

private SqlConnection Conn;
private List<GASusers> Users = new List<GASusers>();
private List<PersonalUsers> Personal = new List<PersonalUsers>();


        private void Run()
        {
            ReadPersonalFile();

            if (MySQLConnection() != false)
            {
                ListofEmplyees();
            }
            Conn.Close();
            Conn.Dispose();
        }

        private void ReadPersonalFile()
        {
            string[] textfile = File.ReadAllLines("NewUsers.txt");
            foreach (string line in textfile)
            {
                string A = line.Replace("\"", "");
                string[] B = A.Split(',');

                PersonalUsers PUsers = new PersonalUsers();
                PUsers.EmployeeNumber = Convert.ToInt32(B[0].ToString().Remove(0, 2));
                PUsers.Firstname = B[1].ToString();
                PUsers.Lastname = B[2].ToString();
                PUsers.EMail = B[3].ToString();

                Personal.Add(PUsers);
            }
        }

        private bool MySQLConnection()
        {
            try
            {
                Conn = new SqlConnection();
                string strConn = "Data Source=localhost;Initial Catalog=AllUsers;Integrated Security=True";
                Conn.ConnectionString = strConn;

                if (Conn.State != ConnectionState.Open)
                {
                    Conn.Open();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }

        private void ListofEmplyees()
        {

            SqlCommand getlist = new SqlCommand();
            getlist.Connection = Conn;
            getlist.CommandText = "SELECT * FROM Employee";

            using (SqlDataReader DR = getlist.ExecuteReader())
            {
                while (DR.Read())
                {
                    DATAusers auser = new DATAusers();
                    auser.EmployeeNumber = Convert.ToInt32(DR["EmployeeNumber"].ToString());
                    auser.Firstname = DR["FirstName"].ToString();
                    auser.Lastname = DR["Lastname"].ToString();
                    auser.EMail = DR["Email"].ToString();
                    //auser. ect for later
                    Users.Add(auser);
                }
            }
        }


This post has been edited by madmorgan: 04 March 2011 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 08:19 AM

I don't see where you attempt to use the .Intersect method or even do some sort of comparison. What have you tried?
Was This Post Helpful? 0
  • +
  • -

#5 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 08:40 AM

I did try but it was not right it keeps highlighting red so i deleted it but here what i did

Microsoft's
IEnumerable<int> both = id1.Intersect(id2);


Mine
IEnumerable<DifferntUsers> both = Users.Intersect(EmployeeNumber);

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5102
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 08:42 AM

Are you on .NET 3.5 or greater, and have a using System.Linq; statement?

This post has been edited by insertAlias: 04 March 2011 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 08:45 AM

using .net 4 visual studio 2010
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5102
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 09:02 AM

And do you have the using statement I mentioned there?
Was This Post Helpful? 0
  • +
  • -

#9 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 09:14 AM

hello all again,

all I want to do is compaire the Personal list<> agents the Users list<> and any thing thats different in to a new list called Diff list<>

private List<DATAusers> Users = new List<DATAusers>();
private List<PersonalUsers> Personal = new List<PersonalUsers>();

in the Users the both have and ID, Name, Last Name, Email address.

i have link but I not sure how to use it to comapaire the two lists and the throw out any thing different to a new list im still new at c# i only learned it about 4 months ago.
Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 09:47 AM

Read the Remarks.
http://msdn.microsof...v=VS.90%29.aspx

MSDN is fantastic, I urge you to read what they write there. It's a massive learning tool.

Quote

The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer<T> generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.


Try reading the articles they link and you will learn more than you even knew would be necessary to complete this. :)

Having said that, I do understand how this might be over your head since you are just starting out, but give it a shot!

It's lunch time, I'll come back later in the afternoon and help you out if you are having trouble.

This post has been edited by Sergio Tapia: 04 March 2011 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#11 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 09:50 AM

Ive even tryed to do this but I cant get this right

int total = Personal.Count;
            int X = 0;
            while ( X <= total)
            {
                foreach (var A in Personal)
                {
                    foreach (var B in Users)
                    {
                        if (A.EmployeeNumber == B.EmployeeNumber)
                        {
                            X++;
                            break;
                        }
                    }
                }
            }

Was This Post Helpful? 0
  • +
  • -

#12 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 10:08 AM

and even tryed this

        private List<DATAusers> Users = new List<DATAusers>();
        private List<PersonalUsers> Personal = new List<PersonalUsers>();
        private List<DifferntUsers> DiffUsers = new List<DifferntUsers>();

            Users.Add(new DATAusers() {EmployeeNumber =1});
            Users.Add(new DATAusers() { EmployeeNumber = 2});

            Personal.Add(new PersonalUsers() { EmployeeNumber = 2});
            Personal.Add(new PersonalUsers() { EmployeeNumber = 1});
            Personal.Add(new PersonalUsers() { EmployeeNumber = 3});
            Personal.Add(new PersonalUsers() { EmployeeNumber = 4});
            Personal.Add(new PersonalUsers() { EmployeeNumber = 5});

            int total = Personal.Count;
            int X = 0;
            while ( X <= total)
            {
                foreach (var A in Personal)
                {
                    foreach (var B in Users)
                    {
                        if (A.EmployeeNumber == B.EmployeeNumber)
                        {
                            X++;
                            break;
                        }
                        else
                        {
                        
                        }
                    }
                    DifferntUsers D = new DifferntUsers();
                    D.EmployeeNumber = A.EmployeeNumber;
                    DiffUsers.Add(D);                    
                }
            }


Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5102
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 10:17 AM

You're over-complicating things:

foreach(var a in Personal){
  bool inUsers = false;
  foreach(var b in Users){
    if(b.EmployeeNumber == a.EmployeeNumber)
      inUsers = true;
  }
  if(inUsers)
    DiffUsers.Add(new DifferentUser() { EmployeeNumber = a.EmployeeNumber };
}


At the end of those loops, DiffUsers should have all the users they have in common.

What I'm curious about is, why have you defined three different classes for users? Shouldn't they all be the same class, and just have different names for the Lists?

This post has been edited by insertAlias: 04 March 2011 - 10:18 AM

Was This Post Helpful? 1
  • +
  • -

#14 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 10:19 AM

I agree with Alias, I thought it was odd that you have different Object types when they essentially have the same attributes according to what you've told us.
Was This Post Helpful? 0
  • +
  • -

#15 madmorgan  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 104
  • Joined: 07-May 10

Re: Help with two list<>'s to compare differences

Posted 04 March 2011 - 10:39 AM

It was my manger's idear to have 3 different classes I just get forced to do what im told. :-(

i just got this to work would this work in the same way?

 foreach(var P in Personal)
            {
                GASusers sobject = Users.Find(delegate(GASusers o){return o.EmployeeNumber == P.EmployeeNumber;
                                                                    return o.Lastname == P.Lastname;});

                if (sobject == null)
                {
                    DifferntUsers D = new DifferntUsers();
                    D.EmployeeNumber = P.EmployeeNumber;
                    D.Lastname = P.Lastname;

                    DiffUsers.Add(D);
                }
            }

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2