Asp .Net LINQ to SQL 数据绑定,执行“查找”没有查找表?
假设我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要再次加入经理的 Employee 表
编辑:
可以很容易地翻译成 LINQ:
You need to join the Employee table again for the manager
EDIT:
Which can be easily translated into LINQ:
好的,是否存在一元关系设置?如果是关系,则 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.