Asp .Net LINQ to SQL 数据绑定,执行“查找”没有查找表?

发布于 2024-10-14 08:57:59 字数 431 浏览 4 评论 0原文

假设我有一个 Employee 表。每个员工都有一个唯一的 ID。表中的一列是 ManagerId,它对应于另一个 Employee。将员工的数据绑定到 GridView 时,我想显示经理的姓名,而不是他们的 ID。如果我有一个 Mangers 查找表,我可以执行 <%# Eval("lu_Managers.ManagerName") %>,但我没有这样的表,我也不想这样做每次添加或删除新经理时制作一个/跟踪它/更新它。

目前在 OnRowDataBound 中,我调用 e.Row.Cells[1].Text = getFullNameFromEmployeeId(Convert.ToInt32(e.Row.Cells[1].Text)); 这对我来说似乎相当混乱。

有没有办法在不使用代码隐藏的情况下做到这一点?或者说这会比我现在的效率低吗?

Say I have an Employee table. Each Employee has a unique id. One of the columns in the table is ManagerId, which corresponds to another Employee. When binding an Employee's data to a GridView, I want to display the Manager's name, not their Id. If I had a lookup table for Mangers I could just do <%# Eval("lu_Managers.ManagerName") %>, but I don't have such a table, nor do I want to make one/keep track of it/update it everytime a new manager is added or removed.

Currently in the OnRowDataBound I call e.Row.Cells[1].Text = getFullNameFromEmployeeId(Convert.ToInt32(e.Row.Cells[1].Text)); which seems fairly messy to me.

Is there a way to do this without using the codebehind? Or would that be less efficient than what I have now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-10-21 08:57:59

您需要再次加入经理的 Employee 表

SELECT 
  Employee.*,
  Manager.FirstName As ManagerName
FROM
  tblEmployee As Employee
  JOIN tblEmployee As Manager
    ON Employee.ManagerID = Manager.pkEmployeeID 

编辑:
可以很容易地翻译成 LINQ:

var q = 
 from employee in db.tblEmployee
 join manager in db.tblEmployee
   on employee.ManagerID equals manager.pkEmployeeID
 select new
 {
   Employee = employee,
   ManagerName = manager.FirstName
 };

You need to join the Employee table again for the manager

SELECT 
  Employee.*,
  Manager.FirstName As ManagerName
FROM
  tblEmployee As Employee
  JOIN tblEmployee As Manager
    ON Employee.ManagerID = Manager.pkEmployeeID 

EDIT:
Which can be easily translated into LINQ:

var q = 
 from employee in db.tblEmployee
 join manager in db.tblEmployee
   on employee.ManagerID equals manager.pkEmployeeID
 select new
 {
   Employee = employee,
   ManagerName = manager.FirstName
 };
他是夢罘是命 2024-10-21 08:57:59

好的,是否存在一元关系设置?如果是关系,则 Employee 将有一个指向该经理的 Employee 属性,因此您可以使用 Employee.EmployeeName 进行引用。否则,如果没有显式导航属性,则必须使用来自代码隐藏的查询。

HTH。

OK, if there a unary relationship setup? If a relationship, Employee would have an Employee property that points to that manager, so you could reference with Employee.EmployeeName. Otherwise, if no explicit navigation property, you would have to use a query from code-behind.

HTH.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文