开发者

converting/Casting ISingleResult - List values to my class without Looping

I have a DataContract like this:

[Serializable]
[DataContract]
public class Employee: IEmployee
{
    public Employee();

    [DataMember]
    public string EmpID { get; set; }
    [DataMember]
    public List<EmpSalaryDetail> EmpSalaryDetails { get; set; }
    [DataMember]
    public List<EmpPersonalDetail> EmpPersonalDetails { get; set; }
}
[DataContract]
public class EmpSalaryDetail: IEmpSalaryDetail
{
    public EmpSalaryDetail();

    [DataMember]
    public string EmpID { get; set; }
    [DataMember]
    public int GrossSal { get; set; }
}
public class EmpPersonalDetail: IEmpPersonalDetail
{
    public EmpPersonalDetail();

    [DataMember]
    public string EmpID { get; set; }
    [DataMember]
    public string EmpName { get; set; }
}

And I'm using Linq to Sql in my c# app which creates the below code:

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetEmployeeDetails")]
public ISingleResult<GetEmployeeDetailsResult> GetEmployeeDetails([global::System.Data.Linq.Mapping.ParameterAttribute(Name="EmpId", DbType="NVarChar(32)")] string empID)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)   
(MethodInfo.GetCurrentMethod())), empID);
return ((ISingleResult<GetEmployeeDetailsResult>)(result.ReturnValue));
}

I'm conver开发者_开发百科ting GetEmployeeDetailsResult the result to a List and I'm getting something like this:

EmpID GrossSal EmpName

a11 3560 raj

a12 9760 kumar

a13 5670 selva

a14 3080 joe

a15 8900 fra

a16 5500 ravi

a17 7000 mani

I want to map this flat List back to an object of Employee class. I don't want to loop through the List and create object for each class.

Is there any other way like writing a linq query again or anything else really smart.

Please give you suggestions.


Something like this should do the trick. You didn't give us the definition for GetEmployeeDetailsResult, so you might have to throw in some calls to Convert.ToInt32 depending on the types of GetEmployeeDetailsResult.EmpID and GetEmployeeDetailsResult.GrossSal:

var employees = 
    result.Select(x => new Employee {
        EmpID = x.EmpID,
        EmpSalaryDetails = new List<EmpSalaryDetail> {
            new EmpSalaryDetail { 
                EmpID = x.EmpID,
                GrossSal = x.GrossSal
            }
        },
        EmpPersonalDetails = new List<EmpPersonalDetail> {
            new EmpPersonalDetail {
                EmpID = x.EmpID,
               EmpName = x.EmpName
            }
        }
    );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜