我有一些代码可以在循环中更改数据库中某些数据的值。我只是想知道首先过滤数据的最有效方法是什么?我举个例子:-
与班级: -
public class myObj
{
int id {get;set;}
string product {get; set;}
string parent{get;set;}
bool received {get;set;}
}
和 DbContext:-
public class myCont:DbContext
{
public DbSet<myObj> myObjs {get;set;}
}
这样做更好吗:-
int[] list;
/* Populate list with a bunch of id numbers found in myOBjs */
myCont data = new myCont();
myObj ob = data.myObjs.Where(o => o.parent == "number1");
foreach(int i in list)
{
ob.First(o => o.id == i && o.received != true).received = true;
}
或者:-
int[] list;
/* Populate list with a bunch of id numbers found in myOBjs */
myCont data = new myCont();
foreach(int i in list)
{
data.myObjs.First(o => o.parent == "number1" && o.id == i && o.received != true).received = true;
}
还是没有区别?