C# Entity Framework: 将 select 结果映射到对象列表
C# Entity Framework: Mapping select results to list of objects
假设我有一个学生对象列表,我想找到他们的学费。他们的学费在数据库中,关键是他们的学生 ID。之后我想将该学费映射到学生对象中。所以我有
Class Student{
int Id;
string Name;
double GPA;
double Tuition;
}
示例数据库元组为 |Id | Tuition | SchoolName| Student Name|
List<Double> GetTuitionsById(List<Student> students)
{
var ids = students.Select(x => x.Id).ToList();
var query = from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select dbStudent.Tuition;
var tuitions = query.ToList();
return tuitions;
}
然后我希望遍历 tuitions
并将其映射到学生列表中,但是 select 查询并没有 return 相同顺序的价格。那么如何将学费从数据库映射到学生对象呢?
尝试。
List<Double> GetTuitionsById(List<Student> students)
{
List<Double> objRes=new List<Double>();
var ids = students.Select(x => x.Id).ToList();
var query = (from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select dbStudent.Tuition).ToLIst();
foreach(var x in query )
{
objRes.Add(Double.Parse(x));
}
return objRes;
}
当您在数据库查询中没有排序时,不保证来自数据库的结果按任何特定顺序排列。要 return 按顺序传递的结果,您需要从数据库中 return Id
以便数据可以映射到初始列表,然后手动创建列表:
List<Double> GetTuitionsById(List<Student> students)
{
var ids = students.Select(x => x.Id).ToList();
// return Id and tuition
var query = from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select new { dbStudent.Id, dbStudent.Tuition};
var tuitions = query.ToList();
var results = List<double>();
foreach(var student in students)
{
// below is assumed that every student has tuition returned from the database
var foundTuition = tuitions.First(t => t.Id == student.Id);
results.Add(foundTuition.Tuition);
}
return results;
}
编辑:
@pwas 正确地指出使用 Dictionary
会提供更好的性能:
List<Double> GetTuitionsById(List<Student> students)
{
var ids = students.Select(x => x.Id).ToList();
// return Id and tuition
var query = from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select new { dbStudent.Id, dbStudent.Tuition};
var tuitions = query.AsEnumerable().ToDictionary(s => s.Id, s => x.Tuition);
var results = List<double>();
foreach(var student in students)
{
// below is assumed that every student has tuition returned from the database
results.Add(tuitions[student.Id]);
}
return results;
}
假设我有一个学生对象列表,我想找到他们的学费。他们的学费在数据库中,关键是他们的学生 ID。之后我想将该学费映射到学生对象中。所以我有
Class Student{
int Id;
string Name;
double GPA;
double Tuition;
}
示例数据库元组为 |Id | Tuition | SchoolName| Student Name|
List<Double> GetTuitionsById(List<Student> students)
{
var ids = students.Select(x => x.Id).ToList();
var query = from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select dbStudent.Tuition;
var tuitions = query.ToList();
return tuitions;
}
然后我希望遍历 tuitions
并将其映射到学生列表中,但是 select 查询并没有 return 相同顺序的价格。那么如何将学费从数据库映射到学生对象呢?
尝试。
List<Double> GetTuitionsById(List<Student> students)
{
List<Double> objRes=new List<Double>();
var ids = students.Select(x => x.Id).ToList();
var query = (from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select dbStudent.Tuition).ToLIst();
foreach(var x in query )
{
objRes.Add(Double.Parse(x));
}
return objRes;
}
当您在数据库查询中没有排序时,不保证来自数据库的结果按任何特定顺序排列。要 return 按顺序传递的结果,您需要从数据库中 return Id
以便数据可以映射到初始列表,然后手动创建列表:
List<Double> GetTuitionsById(List<Student> students)
{
var ids = students.Select(x => x.Id).ToList();
// return Id and tuition
var query = from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select new { dbStudent.Id, dbStudent.Tuition};
var tuitions = query.ToList();
var results = List<double>();
foreach(var student in students)
{
// below is assumed that every student has tuition returned from the database
var foundTuition = tuitions.First(t => t.Id == student.Id);
results.Add(foundTuition.Tuition);
}
return results;
}
编辑:
@pwas 正确地指出使用 Dictionary
会提供更好的性能:
List<Double> GetTuitionsById(List<Student> students)
{
var ids = students.Select(x => x.Id).ToList();
// return Id and tuition
var query = from dbStudent in _context.StudentsDB
where ids.Contains(dbStudent.Id)
select new { dbStudent.Id, dbStudent.Tuition};
var tuitions = query.AsEnumerable().ToDictionary(s => s.Id, s => x.Tuition);
var results = List<double>();
foreach(var student in students)
{
// below is assumed that every student has tuition returned from the database
results.Add(tuitions[student.Id]);
}
return results;
}