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.
ToUpperat allMERGE 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.