在C#中,使用linq如何将列表类型参数传递给方法

本文关键字:列表 类型 参数传递 方法 使用 linq | 更新日期: 2023-09-27 18:25:34

我有一种情况,我需要将数据存储在列表中,然后将相关字段传递给我的方法,我该如何做到这一点,请参阅下面的代码。

public static List<Supplier_Claim_Upload_Result> ExcludeFailedValidationRecords()
    {
        IList<CRMSupplierClaimsData> claimsData = GetClaimsUpdateRecordsFromStaging();
        using (EmbraceEntities context = new EmbraceEntities())
        {
            var validatedList = context.Supplier_Claim_Upload(claimsData.LineNunber, claimsData.TotalClaim);// i get an error over here
            return new List<Supplier_Claim_Upload_Result>(validatedList);
        }
    }

在C#中,使用linq如何将列表类型参数传递给方法

请尝试使用以下代码片段。您遇到错误,因为您试图访问集合中的属性来代替对象。

public static List<Supplier_Claim_Upload_Result> ExcludeFailedValidationRecords()
{
    IList<CRMSupplierClaimsData> claimsData = GetClaimsUpdateRecordsFromStaging();
    List<Supplier_Claim_Upload_Result> lst = new List<Supplier_Claim_Upload_Result>();
    using (EmbraceEntities context = new EmbraceEntities())
    {
        foreach (var item in claimsData)
        {
                lst.AddRange(context.Supplier_Claim_Upload(item.LineNunber, item.TotalClaim));
        }
    }
    return lst;
}

更新1:

public static List<Supplier_Claim_Upload_Result> ExcludeFailedValidationRecords()
{
    IList<CRMSupplierClaimsData> claimsData = GetClaimsUpdateRecordsFromStaging();
    List<Supplier_Claim_Upload_Result> lst = new List<Supplier_Claim_Upload_Result>();
    using (EmbraceEntities context = new EmbraceEntities())
    {
        foreach (var item in claimsData)
        {
            var test = context.Supplier_Claim_Upload(item.LineNunber, item.TotalClaim);
            lst.AddRange(test.ToList<Supplier_Claim_Upload_Result>());
        }
    }
    return lst;
}

更新2:

public static List<Supplier_Claim_Upload_Result> ExcludeFailedValidationRecords()
{
    IList<CRMSupplierClaimsData> claimsData = GetClaimsUpdateRecordsFromStaging();
    List<Supplier_Claim_Upload_Result> lst = new List<Supplier_Claim_Upload_Result>();
    using (EmbraceEntities context = new EmbraceEntities())
    {
        foreach (var item in claimsData)
        {
            var test = context.Supplier_Claim_Upload(item.LineNunber, item.TotalClaim);
            foreach (var childitem in test)
            {
                lst.Add(childitem);   
            }
        }
    }
    return lst;
}

如果有任何顾虑,请告诉我。