我是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类创建的。现在我如何将结果添加到此?请帮帮我。我迫切需要!
EDITED ----- 所以我使用了下面一个人的建议并且它工作了,我试图改进它..下面是我的新代码。
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;
}
public static void employeeEmploy(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);
}
}
在执行此操作时,我在结束时收到以下错误。
对象引用未设置为对象的实例。
描述:执行期间发生了未处理的异常 当前的网络请求。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.NullReferenceException:不是对象引用 设置为对象的实例。
注意 - 它确实打印出正确的数据集。最后得到这个错误。任何理由??
答案 0 :(得分:2)
您无法将匿名对象插入List 简单地做这样的事情:
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
{
position_id = blah.ID,
housing = blah.Housing
};
//I'm making stuff up as the property because I don't understand the stuff you are filling.
//Fill as appropriate
之后:
var y = query.SingleOrDefault();
var basics = new List<BasicPay>();
basics.Add(y);
您无法y.ID
或y.SomeProperty
,因为y
是object
.. object
没有这些属性。所以一些建议的答案是行不通的:(
答案 1 :(得分:2)
怎么样
basic.Add(new BasicPay { employee_personal_id = y.Employee_Personal_InfoEmp_id, grade_id = y.Grade_Id});
答案 2 :(得分:0)
您正在创建BasicPay
列表,因此您应该将此类型的实例放入列表中:
basic.Add(new BasicPay {employee_personal_id = y.Employee_Personal_InfoEmp_id, grade_id = y.Grade_Id});
答案 3 :(得分:0)
我想以下解决方案可以帮助您
BasicPay objPay = new BasicPay();
objPay.employee_personal_id = y.Employee_Personal_InfoEmp_id;
objPay.grade_id = y.Grade_Id;
basic.Add ( objPay );
)
答案 4 :(得分:0)
要么缺少构造函数,要么必须从默认构造函数初始化它:
public BasicPay(int empId, int gradeId)
{
--> here initialization
}
或者当您将所有属性设置为public时,您可以使用默认构造函数:
basic.Add(new BasicPay
{
employee_personal_id=y.Employee_Personal_InfoEmp_id,
grade_id = y.Grade_Id
});