gpt4 book ai didi

c# - IEnumerable 与 IQueryable 对象 - LinQ to SQL

转载 作者:行者123 更新时间:2023-11-30 20:41:02 27 4
gpt4 key购买 nike

起初我有一个名为“Employees”的 IEnumerable 对象,它的所有属性都映射到一个类“Employee”。

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

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

像魅力一样工作。

现在我有一堆映射到数据库的类和一个 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};

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

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

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

我类的样本:-

 [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接口(interface)对底层类型一无所知。您必须使用通用版本 ( IQueryable<T> ) 才能在设计时查看项目的属性。

但是,由于您要投影到匿名 类型(而不是 Employee 对象的集合,尽管您将其命名为变量),但您不知道类型名称编译时,所以你不能指定类型 T用于 IQueryable<T> .

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

类定义类似于:

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 标准 - 大写驼峰式大小写的类和小写驼峰式大小写的变量

关于c# - IEnumerable 与 IQueryable 对象 - LinQ to SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32637985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com