我想将此方法用于不同的类
有没有办法将此变量 Collection 转换为其他方法
因为我有四个类,如RSummary
//ResultQuery : i want to cast this variable more than one Type eg :
var collection = await ResultQuery(entity, _classId).Cast<RSummary>().ToListAsync();
foreach (var result in collection)
{
decimal Percentile = 0;
int Rank = 0;
// Rank
if (result.Percentage == Percentile)
result.Rank = Rank;
else
{
result.Rank = Rank + 1; Percentile = result.Percentage; Rank++;
}
var Values = await GetGrades(result.Percentage); //returns student GPA, Grade, etc.
result.Grade = Values.Grade;
result.Description= Values.Description;
result.GPA = Values.GPA;
Values = null;
entity.Entry(result).State = EntityState.Modified;
}
Rsummary class
public class RSummary
{
public int Id { get; set; }
public int ExamsId { get; set; }
public string StudentsId { get; set; }
public decimal HM { get; set; }
public decimal Marks { get; set; }
public decimal Percentage { get; set; }
public string Grade { get; set; }
public int Rank { get; set; }
public string Result { get; set; }
public decimal GPA { get; set; }
public virtual Exam Exams { get; set; }
public virtual Student Students { get; set; }
}
答案 0 :(得分:3)
假设有共同的属性,您可以使用界面和泛型
执行类似的操作public interface IMyAwesomeInterface
{
decimal Percentage { get; set; }
int Grade { get; set; }
string Description { get; set; }
decimal GPA { get; set; }
int Rank { get; set; }
}
public void SomeMethod<T>(Entity entity, int classId)
where T : IMyAwesomeInterface
{
var collection = await ResultQuery(entity, classId).Cast<T>().ToListAsync();
foreach (var result in collection)
{
decimal Percentile = 0;
int Rank = 0;
if (result.Percentage == Percentile)
result.Rank = Rank;
else
{
result.Rank = Rank + 1;
Percentile = result.Percentage;
Rank++;
}
var Values = await GetGrades(result.Percentage); //returns student GPA, Grade, etc.
result.Grade = Values.Grade;
result.Description = Values.Description;
result.GPA = Values.GPA;
Values = null;
entity.Entry(result).State = EntityState.Modified;
}
}
免责声明:只是基于所提供代码的示例,我对您的业务逻辑和其他问题不承担任何责任,或者对儿童可能造成的伤害