正在创建自定义链接类
本文关键字:链接 自定义 创建 | 更新日期: 2023-09-27 17:59:47
我对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;
}
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);
}
}
在这样做的时候,我在最后得到了以下错误。
对象引用未设置为对象的实例。
描述:在执行期间发生未处理的异常当前web请求。请查看堆栈跟踪以了解更多信息有关错误的信息以及错误在代码中的来源。
异常详细信息:System.NullReferenceException:对象引用不是设置为对象的实例。
注意-它确实打印出了正确的数据集。。只是最后出现了这个错误。有什么原因吗??
怎么样
basic.Add(new BasicPay { employee_personal_id = y.Employee_Personal_InfoEmp_id, grade_id = y.Grade_Id});
不能将匿名对象插入列表简单地做这样的事情:
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
。。CCD_ 5不具有这些属性。因此,一些建议的答案不起作用:(
您正在创建一个BasicPay
的列表,因此您应该将此类型的实例放入列表中:
basic.Add(new BasicPay {employee_personal_id = y.Employee_Personal_InfoEmp_id, grade_id = y.Grade_Id});
我想下面的解决方案将帮助您
BasicPay objPay = new BasicPay();
objPay.employee_personal_id = y.Employee_Personal_InfoEmp_id;
objPay.grade_id = y.Grade_Id;
basic.Add ( objPay );
)
要么您缺少构造函数,要么您必须从默认构造函数初始化它:
public BasicPay(int empId, int gradeId)
{
--> here initialization
}
或者,当你将所有属性设置为公共时,你可以使用默认的构造函数:
basic.Add(new BasicPay
{
employee_personal_id=y.Employee_Personal_InfoEmp_id,
grade_id = y.Grade_Id
});