I am using linq in c# and I have a quick question.
I have shown very minimum amount of code here, in real-time there are also some sorting operations. I would like to know from below which approach should I use??
Approach 1
public class UserDetails
{
private dbContext db = new dbContext();
public List<User> ActiveUser()
{
return db.Users.Where(m => m.Active == true).ToList();
}
public List<User> InActiveUser()
{
return db.Users.Where(m => m.Active == false).ToList();
}
}
Approach 2
public class UserDetails
{
List<Defect> allUser = new db.Users.ToList();
public List<User> ActiveUser()
{
return allUser.Where(m => m.Active == true).ToList();
}
public List<User> InActiveUser()
{
return allUser.Where(m => m.Active == false).ToList();
}
}
There are more than 20 methods which are fetching the data, every method is fetching data from same table with different where condition. My question is Should I create dbContext and then use separate query in every method (Approch 1) OR Should I create one List and fetch all data and filter it in method itself using where condition. (Approch 2)