我对 C# 很陌生。请帮帮我。
class BasicPay
{
public int employee_personal_id { get; set; }
public int grade_id { get; set; }
public int position_id { get; set; }
public Int32 base_pay { get; set; }
public Int32 housing { get; set; }
private static void getEmployeeForEmpFinance(int empPersionalinfoID)
{
var context = new SSPModel.sspEntities();
var query = from c in context.Employee_Financial
join d in context.Employee_Employ on
c.Employee_Personal_InfoEmp_id equals d.Employee_Personal_InfoEmp_id
where c.Employee_Personal_InfoEmp_id == empPersionalinfoID
select new { c.ID, c.Employee_Personal_InfoEmp_id, c.Base_Pay, c.Housing_Allowance, d.Grade_Id };
var y = query.SingleOrDefault();
var basic = new List<BasicPay>();
**basic.Add(y.Employee_Personal_InfoEmp_id,y.Grade_Id);** // THIS IS WHAT I DO NOT UNDERSTAND
}
}
注释行是我没有得到的。我由自己的 BasicPay 类创建。现在如何将我的结果添加到其中?请帮帮我。我急需!
已编辑----- 所以我使用了下面其中一个人的建议并且它起作用了,我试图改进它.. 下面是我的新代码。
private static BasicPay getEmployeeForEmpFinance(int empPersionalinfoID)
{
var context = new SSPModel.sspEntities();
var query = from c in context.Employee_Financial
join d in context.Employee_Employ on
c.Employee_Personal_InfoEmp_id equals d.Employee_Personal_InfoEmp_id
where c.Employee_Personal_InfoEmp_id == empPersionalinfoID
select new BasicPay
{
employee_personal_id = c.Employee_Personal_InfoEmp_id,
grade_id = d.Grade_Id,
base_pay = c.Base_Pay,
housing = c.Housing_Allowance
};
var y = query.SingleOrDefault();
return y;
}
公共静态无效employee(int payperiodID){
var context = new SSPModel.sspEntities();
var query = from c in context.Payroll_Batch
where c.PayperiodID == payperiodID
select c.Payroll_GroupID;
int y = query.SingleOrDefault();
Debug.WriteLine("group id: " + y);
var query2 = from i in context.Employee_Employ
where (i.Payroll_Group == y && i.Grade_Id != null)
select new { i.ID,i.Employee_Personal_InfoEmp_id, i.Date_of_Appointment, i.Date_of_Employment, i.Grade_Id, i.Substantive_designation };
var empEmploy = query2.ToList();
var basics = new List<BasicPay>();
foreach (var x in empEmploy)
{
basics.Add(getEmployeeForEmpFinance(x.Employee_Personal_InfoEmp_id));
}
for (int c=0; c < basics.Count(); c++)
{
Debug.WriteLine(basics.ElementAt(c).employee_personal_id);
}
}
在这样做时,我最后得到以下错误。
你调用的对象是空的。
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。
注意 - 它确实打印出正确的数据集..最后只是得到这个错误。有什么理由吗??