【发布时间】:2011-02-16 18:48:06
【问题描述】:
我已经问过这个问题(“转换/转换 ISingleResult - 将值列表到我的类而没有循环”)但我没有给出正确的例子。所以我用一个正确的例子重复同样的问题:
我有一个这样的 DataContract:
[Serializable]
[DataContract]
public class Employee
{
public Employee()
{
}
[DataMember]
public string Name { get; set; }
[DataMember]
public int EmpId { get; set; }
[DataMember]
public List<EmpMonthSal> EmpMonthlySal { get; set; }
}
public class EmpMonthSal
{
public EmpMonthSal()
{
}
[DataMember]
public int EmpId { get; set; }
[DataMember]
public int Month { get; set; }
[DataMember]
public float Sal { get; set; }
}
我在我的 c# 应用程序中使用 Linq to Sql 来创建以下代码:
[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));
}
自动生成的 GetEmployeeDetailsResult 类如下所示:
public class GetEmployeeDetailsResult
{
public GetEmployeeDetailsResult()
{
}
public string Name { get; set; }
public int EmpId { get; set; }
public int month { get; set; }
public float sal { get; set; }
}
我正在将 GetEmployeeDetailsResult 的 ISingleResult 结果转换为一个列表,我得到这样的结果:
Empname EmpID Month Salary
Raja 1 1 8000
Raja 1 2 8000
Raja 1 3 8000
Raja 1 4 7800
Raja 1 5 7800
Raja 1 6 7800
Raja 1 7 7800
Raja 1 8 7800
Raja 1 9 7800
Raja 1 10 7800
Raja 1 11 7800
Raja 1 12 90000
Kumar 2 1 200000
Kumar 2 2 200000
Kumar 2 3 200000
Kumar 2 4 200000
Kumar 2 5 200000
Kumar 2 6 200000
Kumar 2 7 200000
Kumar 2 8 200000
Kumar 2 9 195000
Kumar 2 10 200000
Kumar 2 11 198000
Kumar 2 12 200000
我想将此平面列表映射回 Employee 类的对象。我不想遍历列表并为每个类创建对象。
有没有其他方法,比如再次编写 linq 查询或其他真正聪明的方法。
请给你建议。
【问题讨论】:
-
虽然某个地方的某个人无疑会想到一些有能力的东西,但我认为它看起来不会太漂亮。
标签: c#