我已经问过这个问题(“转换/转换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#app中使用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结果转换为List,我得到的结果如下:
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类的对象。我不想遍历List并为每个类创建对象。
还有其他方法,例如再次编写linq查询或其他任何非常聪明的查询。
请给你建议。
答案 0 :(得分:2)
试试这个:
List<GetEmployeeDetailsResult> list;
//以某种方式填充你的列表
IEnumerable<Employee> result = list.GroupBy(e => new { id = e.EmpId, name = e.Name })
.Select(e => new Employee
{
EmpId = e.Key.id,
Name = e.Key.name,
EmpMonthlySal = e.Select(ms => new EmpMonthSal
{
EmpId = e.Key.id,
Month = ms.month,
Sal = ms.sal
}).ToList()
});
答案 1 :(得分:0)
使用GetEnumerator。了解更多信息https://www.dotnetperls.com/getenumerator
我将向您解释我的情况,您可以从中寻求帮助。
List<File_Data> liFileData = new List<File_Data>();
try
{
using (var dbContext = new DatavaultClassesDataContext())
{
//Changes for EU setup, Removed sub client id from param for sp
IEnumerator<sp_GetFileListResult> fullList = dbContext.sp_GetFileList(clientId, fileName, archivedFrom, archivedTo, modifiedFrom, modifiedTo, analystStorageAccount,recordDefault).GetEnumerator();
while (fullList.MoveNext())
{
File_Data fData = new File_Data();
fData.Archived_By = fullList.Current.Archived_By;
fData.Archived_On = fullList.Current.Archived_On.ToString("MM/dd/yyyy hh:mm:ss tt");
fData.Client_Name = fullList.Current.Client_Name;
liFileData.Add(fData);
}
}
}
答案 2 :(得分:0)
如果您的类对应于ISingleResult,则可以选择Cast方法
YourModel model = DataContext1.GetEmployeeDetails(empID).Cast<YourModel>();
确保您的模型与函数返回结果相同;