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

时间:2015-12-03 14:56:37

标签: c# linq

我遇到需要在列表中存储数据并将相关字段传递给我的方法的情况,我该怎么做,请参阅下面的代码。

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

1 个答案:

答案 0 :(得分: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)
        {
                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;
}

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