1

1- I I load list of Persons from CSV file and store it in list I load also list of Persons from db and store it in List

how can i get the users which they are in the csv file but not in the databse?

For example: DB has 5 Users (A,B,C,D,E) , CSV has 4 users (B,E,Q,P) the code should return list of users (Q,P)

public class Person
{
    public string UserName { get; set; }
    public int Age { get; set; }
}



 public class App
        {
            private IEnumerable<Person> usersDb;
            private IEnumerable<Person> usersCsv;

            public App(int age)
        {
             usersDb = GetUsersFromDb(age);
             usersCsv = GetUsersFromCsv(age);
        }

        public void AddMissingUsers()
        {
            var missingUsers = usersCsv.Where(x => !usersDb.Any(y => x.UserName.Trim().ToUpper().
                Equals(y.UserName.Trim().ToUpper()))); //I tried this one 
            // add to database
        }

    }

Additional question

Ps: I'm using entity framwork 6 is there a better way to perform the task shown in my code? instate of getting all users with from database then check if exists?

In the other hand, if i do the opposite and check each row in my csv file if exists in databse, this is also can be long operation since the file can contain many rows.

2
  • What is wrong with your attempt? It doesn't look obviously wrong. Also, case sensitivity is usually defined in the database's collation, so you may not need to call ToUpper at all Commented Mar 23, 2016 at 2:25
  • 1
    You might also look into MERGE in SQL Server - which means you dump the new file into a temporary table, and merge the results (in this case, simply insert when not matched), then drop the temporary table. Very fast for large amounts of data. Commented Mar 23, 2016 at 2:26

2 Answers 2

1

You may be able to use Except in this case.

 var usersNotInDb = usersCsv.Except(usersDb).ToList();
 context.Users.AddRange(usersNotInDb);
 context.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

1

This should solve your problem, Change your Person class as follows,

public class Person : IEqualityComparer<Person>
    {
        public string UserName { get; set; }
        public int Age { get; set; }

        bool IEqualityComparer<Person>.Equals(Person x, Person y)
        {
            if (x.UserName == y.UserName)
                return true;
            return false;
        }

        int IEqualityComparer<Person>.GetHashCode(Person obj)
        {
            return base.GetHashCode();
        }
    }

Then change your query to,

Person p = new Person();
        var notInDBList = usersCsv.Except(usersDb, p).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.