【问题标题】:Converting ISingleResult List values to my class without Looping将 ISingleResult 列表值转换为我的类而不循环
【发布时间】: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#


【解决方案1】:

试试这个:

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()
                    });

【讨论】:

  • 非常感谢它的工作。我刚刚添加了 Distinct: Distinct((a, b) => a.EmpId == b.EmpId, c => c.EmpId.GetHashCode()).ToList();
【解决方案2】:

使用 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);
                }
            }
        }

【讨论】:

    【解决方案3】:

    如果你的类对应的是ISingleResult,你可以选择Cast方法

     YourModel model = DataContext1.GetEmployeeDetails(empID).Cast<YourModel>();
    

    确保您的模型与您的函数返回结果相同;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2020-03-29
      相关资源
      最近更新 更多