public IEnumerable <User> GetAll() { using (var db = new SqlConnection(ConnectionString)) { var users = db.Query <User>("select username, password, id from users").ToList(); var targets = new TargetRepository().GetAll(); foreach (var user in users) { var matchingTargets = targets.Where(target => target.UserId == user.Id).ToList(); user.Targets = matchingTargets; } //var targets = new TargetRepository().GetAll().GroupBy(target => target.UserId); //foreach (var user in users) //{ // var matchingTargets = targets.FirstOrDefault(grouping => grouping.Key == user.Id); // user.Targets = matchingTargets?.ToList(); //} return(users); } }
public UserRepository(TargetRepository targetRepository) { _targetRepository = targetRepository; }
public UserRepository(TargetRepository targetRepository, IOptions <DbConfiguration> dbConfig) { _targetRepository = targetRepository; _connectionString = dbConfig.Value.ConnectionString; }