0

我对 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:对象引用未设置为对象的实例。

注意 - 它确实打印出正确的数据集..最后只是得到这个错误。有什么理由吗??

4

5 回答 5

2

You can't insert an anonymous object into a List Simply do something like so:

 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

Then after:

var y = query.SingleOrDefault();
var basics = new List<BasicPay>();
basics.Add(y);

You can't do y.ID or y.SomeProperty because y is an object.. an object doesn't have those properties. So some of the suggested answers won't work :(

于 2013-02-18T13:01:55.413 回答
2

How about

basic.Add(new BasicPay { employee_personal_id = y.Employee_Personal_InfoEmp_id, grade_id = y.Grade_Id});
于 2013-02-18T13:02:51.520 回答
0

You are creating a list of BasicPay so you should put instances of this type into the list:

basic.Add(new BasicPay {employee_personal_id = y.Employee_Personal_InfoEmp_id, grade_id = y.Grade_Id});
于 2013-02-18T13:03:12.987 回答
0

I guess the below solution will help you

BasicPay objPay = new BasicPay();
   objPay.employee_personal_id  = y.Employee_Personal_InfoEmp_id;
   objPay.grade_id              = y.Grade_Id;

   basic.Add ( objPay );

)

于 2013-02-18T13:04:17.847 回答
0

您要么缺少构造函数,要么必须从默认构造函数初始化它:

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
});
于 2013-02-18T13:05:53.177 回答