Reputation: 1
I have asked this question already ("converting/Casting ISingleResult - List values to my class without Looping") but I didin't give a correct example. so I'm repeating the same question with a correct example:
I have a DataContract like this:
[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; }
}
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));
}
And the auto generated GetEmployeeDetailsResult class looks like this:
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; }
}
I'm converting the ISingleResult result of GetEmployeeDetailsResult to a List and I'm getting something like this:
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
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.
Upvotes: 0
Views: 3610
Reputation: 880
if your class is corresponding to the ISingleResult, you can choose the Cast method
YourModel model = DataContext1.GetEmployeeDetails(empID).Cast<YourModel>();
be sure your model is same as your function return result;
Upvotes: 0
Reputation: 11
Use GetEnumerator. Fore more info https://www.dotnetperls.com/getenumerator
I'll explain you my case and you can take help from this.
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);
}
}
}
Upvotes: 0
Reputation: 930
Try this:
List<GetEmployeeDetailsResult> list;
// populate your list somehow
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()
});
Upvotes: 2