如何调用IEnumerable函数

本文关键字:IEnumerable 函数 调用 何调用 | 更新日期: 2023-09-27 18:29:41

我有这种类型的函数。现在我不知道如何调用它来从字段中获取数据。

public static IEnumerable GetMaterialSearch(int reqNo)
    {
        DataClassesCRMDataContext dbContext = new DataClassesCRMDataContext();
        try
        {
            var res = from tbl1 in dbContext.MaterialApplicants
                      join tbl2 in dbContext.MaterialRequests on tbl1.ApplicantID equals tbl2.Applicant
                      where tbl2.RCD_ID == reqNo
                      select new
                      {
                          Crusher = tbl2.Crusher,
                          ApplicantID = tbl2.Applicant,
                          Comments = tbl2.Comments,
                          ReqDate = tbl2.ReqDate,
                          Operator = tbl2.Operator,
                          Title = tbl1.Title,
                          Applicant = tbl1.Applicant,
                          Address = tbl1.Address,
                          Nationality = tbl1.Nationality,
                          HouseNo = tbl1.HouseNo,
                          MobileNo = tbl1.MobileNo,
                      };
            if (res.Count() > 0)
            {
                return res.ToList();  
            }
            return null;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
            return null;
        }
        finally
        {
            dbContext.Connection.Close();
        }
    }

如何调用IEnumerable函数

IEnumerable是一个通常用于迭代的序列:

var result = GetMaterialSearch(42);
if (result != null)
    foreach (var entry in result)
        DoSomething(entry);

编辑:如上所述,代码的问题在于您在IEnumerable结果中返回了一个匿名类型。匿名类型不打算跨越方法边界。来自匿名类型(C#编程指南):

不能将字段、属性、事件或方法的返回类型声明为具有匿名类型。[…]若要将匿名类型或包含匿名类型的集合作为参数传递给方法,可以将该参数声明为类型对象。然而,这样做违背了强键入的目的。如果必须存储查询结果或在方法边界之外传递查询结果,请考虑使用普通的命名结构或类,而不是匿名类型。

如果你绝对不想创建一个命名类,你可以使用反射来访问你的字段,比如通过C#4:中引入的dynamic关键字

var result = GetMaterialSearch(42);
if (result != null)
    foreach (dynamic entry in result)
        Console.WriteLine(entry.ID);

您的方法返回非泛型IEnumerable,这就是问题所在。您应该将其更改为通用IEnumerable<T>,但这将需要创建另一个类:

class MaterialItem
{
    public string Crusher { get; set; }
    public int ApplicantID { get; set; }
    // (...)
}

然后更改您的方法签名:

public static IEnumerable<MaterialItem> GetMaterialSearch(int reqNo)

并更改查询以返回MaterialItem s,而不是匿名类型的对象:

        var res = from tbl1 in dbContext.MaterialApplicants
                  join tbl2 in dbContext.MaterialRequests on tbl1.ApplicantID equals tbl2.Applicant
                  where tbl2.RCD_ID == reqNo
                  select new MaterialItem
                  {
                      Crusher = tbl2.Crusher,
                      ApplicantID = tbl2.Applicant,
                      Comments = tbl2.Comments,
                      ReqDate = tbl2.ReqDate,
                      Operator = tbl2.Operator,
                      Title = tbl1.Title,
                      Applicant = tbl1.Applicant,
                      Address = tbl1.Address,
                      Nationality = tbl1.Nationality,
                      HouseNo = tbl1.HouseNo,
                      MobileNo = tbl1.MobileNo,
                  };

但这并不是我要做的唯一改变。稍后调用Count()来调用ToList()会导致不必要的DB调用。

我会选择:

var results = res.ToList();
if(results.Any())
    return results;
return null;