linq to sql C#:用字符串值替换外键并保留表结构
我想知道是否可以这样做。问题是我试图查询数据库以填充一些列表。如果我要使用此查询:
var query =
a in db.Record_HoldData
where a.HoldStatus == "Open"
select a;
它将返回所有相关数据,并且我仍然可以从结果对象中访问和转换数据,例如:
DateTime time = (DateTime)query.DateOpened;
但是字段 Flavor 是外键数字,因此在这种情况下它毫无用处。我想要做的是完整地返回一个表,用风味查找表中的实际名称替换风味的外键,并且仍然能够使用该数据。这有道理吗?我认为非常简单。
I'm wondering if its possible to do this. The issue being that im trying to query the database to populate some lists. If i were to use this query:
var query =
a in db.Record_HoldData
where a.HoldStatus == "Open"
select a;
It would return all of the relevant data and I could still access and cast the data from the result object like:
DateTime time = (DateTime)query.DateOpened;
But the field Flavor is a foreign key number so its useless in this context. What I want to do is return a table intact, replacing the foreign key of flavor with the actual name from the flavor lookup table and still be able to use that data. Does that make sense? Pretty straight forward, i think.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要像这样连接两个表:
另外,添加 ToList()。否则,您最终将在每次访问查询时查询数据库,而不是加载一次数据并重复使用它。
You need to join two tables like this:
Also, add ToList(). Otherwise you will end up querying database every time you access the query, versus loading data once and reusing it.