IEnumerable 与 IQueryable 对象 - LinQ to SQL

IEnumerable vs IQueryable object - LinQ to SQL

起初我有一个名为 "Employees" 的 IEnumerable 对象,它的所有属性都映射到单个 class "Employee".

我将 IEnumerable 对象 Employee 传递给一个私有方法,该方法解析属性并将其映射到数据-table。

   private void createEmployeesDataTable(IEnumerable<Employee> Employees)
    {
    .... stuff here to define the datatable ....

       foreach (var elem in Employees)
       {
           var row = dataTable.NewRow();
           row["Name"] = elem.JobTitle;
           row["Address"] = elem.Address;
           row["Phone"] = elem.Phone;
           row["DateOfHire"] = elem.HireDate;
           dataTable.Rows.Add(row);
       }
    }

工作得很好。

现在我有一堆 classes,映射到数据库和一个 IQueryable 对象 Employees。代码很简单。

    DataContext db = new DataContext(System.Configuration.ConfigurationManager.ConnectionStrings["employeeDB"].ConnectionString);
    Table<Employee> Emp = db.GetTable<Employee>();
    Table<Address> Add = db.GetTable<Address>();
    Table<BusinessEntityAddress> BE = db.GetTable<BusinessEntityAddress>();
    Table<Phone> Phone = db.GetTable<Phone>();
    Table<State> State = db.GetTable<State>();

    var Employees =
        from Employi in Emp
        join PhoneNumber in Phone on Employi.BusinessEntityID equals PhoneNumber.BusinessEntityID
        join BusEntity in BE on Employi.BusinessEntityID equals BusEntity.BusinessEntityID
        join Addy in Add on BusEntity.AddressID equals Addy.AddressID
        join StProv in State on Addy.StateProvinceID equals StProv.StateProvinceID
        where Employi.HireDate > userInputLimit
        select new {DateOfHire = Employi.HireDate, Address = Addy.AddressLine1 + Addy.City + StProv.StateProvinceCode + Addy.PostalCode,
                    Phone = PhoneNumber.PhoneNumber, Name = Employi.JobTitle};

现在我将这个对象传递给私有方法,以创建一个 Data-table,只是我将它作为匿名类型的 IQueryable 传递。原因是,我的对象现在具有从多个 classes 派生的属性,不再是从单个员工 class:-

private void createEmployeesDataTable(IQueryable Employees)
        {
.... how can I still access all of its properties and bind it to the datatable?? .... 
}

当我在 IQueryable Employees 对象上放置断点时,我可以看到它正确存储了所有 属性 名称和值。但我似乎无法通过代码访问它们....

我的样本 class:-

 [Table(Name="HumanResources.Employee")]
    public class Employee
    {
        private int _BusinessEntityID;
        [Column(IsPrimaryKey = true, CanBeNull=false, Storage = "_BusinessEntityID")]
        public int BusinessEntityID
        {
            get
            {
                return this._BusinessEntityID;
            }
            set
            {
                this._BusinessEntityID = value;
            }

        }

        private string _JobTitle;
        [Column(Storage = "_JobTitle", CanBeNull=false, DbType="nvarchar(50) NOT NULL")]
        public string JobTitle
        {
            get
            {
                return this._JobTitle;
            }
            set
            {
                this._JobTitle = value;
            }
        }


        private DateTime _HireDate;
        [Column(Storage = "_HireDate", CanBeNull=false)]
        public DateTime HireDate
        {
            get
            {
                return this._HireDate;
            }
            set
            {
                this._HireDate = value;
            }
        }
    }

When I put a breakpoint on the IQueryable Employees object, I can see it has all the property names and values correctly stored in it. But I cannot seem to access them via code.

那是因为 非泛型 IQueryable 接口对底层类型一无所知。您必须使用通用版本 (IQueryable<T>) 才能在设计时查看项目的属性。

但是,由于您要投影到 anonymous 类型(而不是 Employee 对象的集合,尽管您将其命名为变量),您不需要在编译时不知道类型名称,因此您无法指定要用于 IQueryable<T>.

的类型 T

最佳 解决方案是定义一个具体类型而不是使用匿名 on,这样您就可以在编译时访问该字段。您 可以 使用 dynamic,但是您将 属性 绑定延迟到 运行-time 并且不会在编译时捕获任何错误-时间。

class 定义类似于:

public class EmployeeView
{
    public DateTime DateOfHire {get; set;}
    public string AddressLine1 {get; set;}
    public string City {get; set;}
    public string StateProvinceCode {get; set;}
    public string PostalCode {get; set;}
    public string Phone {get; set;}
    public string Name {get; set;}
}

你的预测是:

select new EmployeeView {
    DateOfHire = Employi.HireDate, 
    AddressLine1 = Addy.AddressLine1,
    City = Addy.City,
    StateProvinceCode = StProv.StateProvinceCode,
    PostalCode = Addy.PostalCode,
    Phone = PhoneNumber.PhoneNumber, 
    Name = Employi.JobTitle};

现在您可以在 :

中指定类型
private void CreateEmployeesDataTable(IEnumerable<EmployeeView> employees)
{
    .... 
}

请注意,我已将大小写更改为 .NET 标准 - classes 使用大写驼峰式大小写,变量使用小写驼峰式大小写